
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta charset="utf-8" /><meta name="generator" content="Docutils 0.17.1: http://docutils.sourceforge.net/" />

    <title>ast — Abstract Syntax Trees &#8212; Python 3.9.6 documentation</title>
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python 3.9.6 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="next" title="symtable — Access to the compiler’s symbol tables" href="symtable.html" />
    <link rel="prev" title="parser — Access Python parse trees" href="parser.html" />
    <link rel="canonical" href="https://docs.python.org/3/library/ast.html" />
    
      
      
    

    
    <style>
      @media only screen {
        table.full-width-table {
            width: 100%;
        }
      }
    </style>

    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
     


  </head><body>
  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="symtable.html" title="symtable — Access to the compiler’s symbol tables"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="parser.html" title="parser — Access Python parse trees"
             accesskey="P">previous</a> |</li>

    <li><img src="../_static/py.png" alt=""
             style="vertical-align: middle; margin-top: -1px"/></li>
    <li><a href="https://www.python.org/">Python</a> &#187;</li>
    

    <li>
      <a href="../index.html">3.9.6 Documentation</a> &#187;
    </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="language.html" accesskey="U">Python Language Services</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="Quick search" type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <section id="module-ast">
<span id="ast-abstract-syntax-trees"></span><h1><a class="reference internal" href="#module-ast" title="ast: Abstract Syntax Tree classes and manipulation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ast</span></code></a> — Abstract Syntax Trees<a class="headerlink" href="#module-ast" title="Permalink to this headline">¶</a></h1>
<p><strong>Source code:</strong> <a class="reference external" href="https://github.com/python/cpython/tree/3.9/Lib/ast.py">Lib/ast.py</a></p>
<hr class="docutils" />
<p>The <a class="reference internal" href="#module-ast" title="ast: Abstract Syntax Tree classes and manipulation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ast</span></code></a> module helps Python applications to process trees of the Python
abstract syntax grammar.  The abstract syntax itself might change with each
Python release; this module helps to find out programmatically what the current
grammar looks like.</p>
<p>An abstract syntax tree can be generated by passing <a class="reference internal" href="#ast.PyCF_ONLY_AST" title="ast.PyCF_ONLY_AST"><code class="xref py py-data docutils literal notranslate"><span class="pre">ast.PyCF_ONLY_AST</span></code></a> as
a flag to the <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a> built-in function, or using the <a class="reference internal" href="#ast.parse" title="ast.parse"><code class="xref py py-func docutils literal notranslate"><span class="pre">parse()</span></code></a>
helper provided in this module.  The result will be a tree of objects whose
classes all inherit from <a class="reference internal" href="#ast.AST" title="ast.AST"><code class="xref py py-class docutils literal notranslate"><span class="pre">ast.AST</span></code></a>.  An abstract syntax tree can be
compiled into a Python code object using the built-in <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a> function.</p>
<section id="abstract-grammar">
<span id="id1"></span><h2>Abstract Grammar<a class="headerlink" href="#abstract-grammar" title="Permalink to this headline">¶</a></h2>
<p>The abstract grammar is currently defined as follows:</p>
<div class="highlight-asdl notranslate"><div class="highlight"><pre><span></span><span class="c c-Singleline">-- ASDL&#39;s 4 builtin types are:</span>
<span class="c c-Singleline">-- identifier, int, string, constant</span>

<span class="k">module</span> <span class="nt">Python</span>
<span class="p">{</span>
    <span class="n">mod</span> <span class="o">=</span> <span class="nc">Module</span><span class="p">(</span><span class="bp">stmt</span><span class="o">* </span><span class="n">body</span>, <span class="bp">type_ignore</span><span class="o">* </span><span class="n">type_ignores</span><span class="p">)</span>
        <span class="o">|</span> <span class="nc">Interactive</span><span class="p">(</span><span class="bp">stmt</span><span class="o">* </span><span class="n">body</span><span class="p">)</span>
        <span class="o">|</span> <span class="nc">Expression</span><span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">body</span><span class="p">)</span>
        <span class="o">|</span> <span class="nc">FunctionType</span><span class="p">(</span><span class="bp">expr</span><span class="o">* </span><span class="n">argtypes</span>, <span class="bp">expr</span><span class="o"> </span><span class="n">returns</span><span class="p">)</span>

    <span class="n">stmt</span> <span class="o">=</span> <span class="nc">FunctionDef</span><span class="p">(</span><span class="bp">identifier</span><span class="o"> </span><span class="n">name</span>, <span class="bp">arguments</span><span class="o"> </span><span class="n">args</span>,
                       <span class="bp">stmt</span><span class="o">* </span><span class="n">body</span>, <span class="bp">expr</span><span class="o">* </span><span class="n">decorator_list</span>, <span class="bp">expr</span><span class="o">? </span><span class="n">returns</span>,
                       <span class="bp">string</span><span class="o">? </span><span class="n">type_comment</span><span class="p">)</span>
          <span class="o">|</span> <span class="nc">AsyncFunctionDef</span><span class="p">(</span><span class="bp">identifier</span><span class="o"> </span><span class="n">name</span>, <span class="bp">arguments</span><span class="o"> </span><span class="n">args</span>,
                             <span class="bp">stmt</span><span class="o">* </span><span class="n">body</span>, <span class="bp">expr</span><span class="o">* </span><span class="n">decorator_list</span>, <span class="bp">expr</span><span class="o">? </span><span class="n">returns</span>,
                             <span class="bp">string</span><span class="o">? </span><span class="n">type_comment</span><span class="p">)</span>

          <span class="o">|</span> <span class="nc">ClassDef</span><span class="p">(</span><span class="bp">identifier</span><span class="o"> </span><span class="n">name</span>,
             <span class="bp">expr</span><span class="o">* </span><span class="n">bases</span>,
             <span class="bp">keyword</span><span class="o">* </span><span class="n">keywords</span>,
             <span class="bp">stmt</span><span class="o">* </span><span class="n">body</span>,
             <span class="bp">expr</span><span class="o">* </span><span class="n">decorator_list</span><span class="p">)</span>
          <span class="o">|</span> <span class="nc">Return</span><span class="p">(</span><span class="bp">expr</span><span class="o">? </span><span class="n">value</span><span class="p">)</span>

          <span class="o">|</span> <span class="nc">Delete</span><span class="p">(</span><span class="bp">expr</span><span class="o">* </span><span class="n">targets</span><span class="p">)</span>
          <span class="o">|</span> <span class="nc">Assign</span><span class="p">(</span><span class="bp">expr</span><span class="o">* </span><span class="n">targets</span>, <span class="bp">expr</span><span class="o"> </span><span class="n">value</span>, <span class="bp">string</span><span class="o">? </span><span class="n">type_comment</span><span class="p">)</span>
          <span class="o">|</span> <span class="nc">AugAssign</span><span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">target</span>, <span class="bp">operator</span><span class="o"> </span><span class="n">op</span>, <span class="bp">expr</span><span class="o"> </span><span class="n">value</span><span class="p">)</span>
          <span class="c c-Singleline">-- &#39;simple&#39; indicates that we annotate simple name without parens</span>
          <span class="o">|</span> <span class="nc">AnnAssign</span><span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">target</span>, <span class="bp">expr</span><span class="o"> </span><span class="n">annotation</span>, <span class="bp">expr</span><span class="o">? </span><span class="n">value</span>, <span class="bp">int</span><span class="o"> </span><span class="n">simple</span><span class="p">)</span>

          <span class="c c-Singleline">-- use &#39;orelse&#39; because else is a keyword in target languages</span>
          <span class="o">|</span> <span class="nc">For</span><span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">target</span>, <span class="bp">expr</span><span class="o"> </span><span class="n">iter</span>, <span class="bp">stmt</span><span class="o">* </span><span class="n">body</span>, <span class="bp">stmt</span><span class="o">* </span><span class="n">orelse</span>, <span class="bp">string</span><span class="o">? </span><span class="n">type_comment</span><span class="p">)</span>
          <span class="o">|</span> <span class="nc">AsyncFor</span><span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">target</span>, <span class="bp">expr</span><span class="o"> </span><span class="n">iter</span>, <span class="bp">stmt</span><span class="o">* </span><span class="n">body</span>, <span class="bp">stmt</span><span class="o">* </span><span class="n">orelse</span>, <span class="bp">string</span><span class="o">? </span><span class="n">type_comment</span><span class="p">)</span>
          <span class="o">|</span> <span class="nc">While</span><span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">test</span>, <span class="bp">stmt</span><span class="o">* </span><span class="n">body</span>, <span class="bp">stmt</span><span class="o">* </span><span class="n">orelse</span><span class="p">)</span>
          <span class="o">|</span> <span class="nc">If</span><span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">test</span>, <span class="bp">stmt</span><span class="o">* </span><span class="n">body</span>, <span class="bp">stmt</span><span class="o">* </span><span class="n">orelse</span><span class="p">)</span>
          <span class="o">|</span> <span class="nc">With</span><span class="p">(</span><span class="bp">withitem</span><span class="o">* </span><span class="n">items</span>, <span class="bp">stmt</span><span class="o">* </span><span class="n">body</span>, <span class="bp">string</span><span class="o">? </span><span class="n">type_comment</span><span class="p">)</span>
          <span class="o">|</span> <span class="nc">AsyncWith</span><span class="p">(</span><span class="bp">withitem</span><span class="o">* </span><span class="n">items</span>, <span class="bp">stmt</span><span class="o">* </span><span class="n">body</span>, <span class="bp">string</span><span class="o">? </span><span class="n">type_comment</span><span class="p">)</span>

          <span class="o">|</span> <span class="nc">Raise</span><span class="p">(</span><span class="bp">expr</span><span class="o">? </span><span class="n">exc</span>, <span class="bp">expr</span><span class="o">? </span><span class="n">cause</span><span class="p">)</span>
          <span class="o">|</span> <span class="nc">Try</span><span class="p">(</span><span class="bp">stmt</span><span class="o">* </span><span class="n">body</span>, <span class="bp">excepthandler</span><span class="o">* </span><span class="n">handlers</span>, <span class="bp">stmt</span><span class="o">* </span><span class="n">orelse</span>, <span class="bp">stmt</span><span class="o">* </span><span class="n">finalbody</span><span class="p">)</span>
          <span class="o">|</span> <span class="nc">Assert</span><span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">test</span>, <span class="bp">expr</span><span class="o">? </span><span class="n">msg</span><span class="p">)</span>

          <span class="o">|</span> <span class="nc">Import</span><span class="p">(</span><span class="bp">alias</span><span class="o">* </span><span class="n">names</span><span class="p">)</span>
          <span class="o">|</span> <span class="nc">ImportFrom</span><span class="p">(</span><span class="bp">identifier</span><span class="o">? </span><span class="n">module</span>, <span class="bp">alias</span><span class="o">* </span><span class="n">names</span>, <span class="bp">int</span><span class="o">? </span><span class="n">level</span><span class="p">)</span>

          <span class="o">|</span> <span class="nc">Global</span><span class="p">(</span><span class="bp">identifier</span><span class="o">* </span><span class="n">names</span><span class="p">)</span>
          <span class="o">|</span> <span class="nc">Nonlocal</span><span class="p">(</span><span class="bp">identifier</span><span class="o">* </span><span class="n">names</span><span class="p">)</span>
          <span class="o">|</span> <span class="nc">Expr</span><span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">value</span><span class="p">)</span>
          <span class="o">|</span> <span class="nc">Pass</span> <span class="o">|</span> <span class="nc">Break</span> <span class="o">|</span> <span class="nc">Continue</span>

          <span class="c c-Singleline">-- col_offset is the byte offset in the utf8 string the parser uses</span>
          <span class="nb">attributes</span> <span class="p">(</span><span class="bp">int</span><span class="o"> </span><span class="n">lineno</span>, <span class="bp">int</span><span class="o"> </span><span class="n">col_offset</span>, <span class="bp">int</span><span class="o">? </span><span class="n">end_lineno</span>, <span class="bp">int</span><span class="o">? </span><span class="n">end_col_offset</span><span class="p">)</span>

          <span class="c c-Singleline">-- BoolOp() can use left &amp; right?</span>
    <span class="n">expr</span> <span class="o">=</span> <span class="nc">BoolOp</span><span class="p">(</span><span class="bp">boolop</span><span class="o"> </span><span class="n">op</span>, <span class="bp">expr</span><span class="o">* </span><span class="n">values</span><span class="p">)</span>
         <span class="o">|</span> <span class="nc">NamedExpr</span><span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">target</span>, <span class="bp">expr</span><span class="o"> </span><span class="n">value</span><span class="p">)</span>
         <span class="o">|</span> <span class="nc">BinOp</span><span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">left</span>, <span class="bp">operator</span><span class="o"> </span><span class="n">op</span>, <span class="bp">expr</span><span class="o"> </span><span class="n">right</span><span class="p">)</span>
         <span class="o">|</span> <span class="nc">UnaryOp</span><span class="p">(</span><span class="bp">unaryop</span><span class="o"> </span><span class="n">op</span>, <span class="bp">expr</span><span class="o"> </span><span class="n">operand</span><span class="p">)</span>
         <span class="o">|</span> <span class="nc">Lambda</span><span class="p">(</span><span class="bp">arguments</span><span class="o"> </span><span class="n">args</span>, <span class="bp">expr</span><span class="o"> </span><span class="n">body</span><span class="p">)</span>
         <span class="o">|</span> <span class="nc">IfExp</span><span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">test</span>, <span class="bp">expr</span><span class="o"> </span><span class="n">body</span>, <span class="bp">expr</span><span class="o"> </span><span class="n">orelse</span><span class="p">)</span>
         <span class="o">|</span> <span class="nc">Dict</span><span class="p">(</span><span class="bp">expr</span><span class="o">* </span><span class="n">keys</span>, <span class="bp">expr</span><span class="o">* </span><span class="n">values</span><span class="p">)</span>
         <span class="o">|</span> <span class="nc">Set</span><span class="p">(</span><span class="bp">expr</span><span class="o">* </span><span class="n">elts</span><span class="p">)</span>
         <span class="o">|</span> <span class="nc">ListComp</span><span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">elt</span>, <span class="bp">comprehension</span><span class="o">* </span><span class="n">generators</span><span class="p">)</span>
         <span class="o">|</span> <span class="nc">SetComp</span><span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">elt</span>, <span class="bp">comprehension</span><span class="o">* </span><span class="n">generators</span><span class="p">)</span>
         <span class="o">|</span> <span class="nc">DictComp</span><span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">key</span>, <span class="bp">expr</span><span class="o"> </span><span class="n">value</span>, <span class="bp">comprehension</span><span class="o">* </span><span class="n">generators</span><span class="p">)</span>
         <span class="o">|</span> <span class="nc">GeneratorExp</span><span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">elt</span>, <span class="bp">comprehension</span><span class="o">* </span><span class="n">generators</span><span class="p">)</span>
         <span class="c c-Singleline">-- the grammar constrains where yield expressions can occur</span>
         <span class="o">|</span> <span class="nc">Await</span><span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">value</span><span class="p">)</span>
         <span class="o">|</span> <span class="nc">Yield</span><span class="p">(</span><span class="bp">expr</span><span class="o">? </span><span class="n">value</span><span class="p">)</span>
         <span class="o">|</span> <span class="nc">YieldFrom</span><span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">value</span><span class="p">)</span>
         <span class="c c-Singleline">-- need sequences for compare to distinguish between</span>
         <span class="c c-Singleline">-- x &lt; 4 &lt; 3 and (x &lt; 4) &lt; 3</span>
         <span class="o">|</span> <span class="nc">Compare</span><span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">left</span>, <span class="bp">cmpop</span><span class="o">* </span><span class="n">ops</span>, <span class="bp">expr</span><span class="o">* </span><span class="n">comparators</span><span class="p">)</span>
         <span class="o">|</span> <span class="nc">Call</span><span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">func</span>, <span class="bp">expr</span><span class="o">* </span><span class="n">args</span>, <span class="bp">keyword</span><span class="o">* </span><span class="n">keywords</span><span class="p">)</span>
         <span class="o">|</span> <span class="nc">FormattedValue</span><span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">value</span>, <span class="bp">int</span><span class="o">? </span><span class="n">conversion</span>, <span class="bp">expr</span><span class="o">? </span><span class="n">format_spec</span><span class="p">)</span>
         <span class="o">|</span> <span class="nc">JoinedStr</span><span class="p">(</span><span class="bp">expr</span><span class="o">* </span><span class="n">values</span><span class="p">)</span>
         <span class="o">|</span> <span class="nc">Constant</span><span class="p">(</span><span class="bp">constant</span><span class="o"> </span><span class="n">value</span>, <span class="bp">string</span><span class="o">? </span><span class="n">kind</span><span class="p">)</span>

         <span class="c c-Singleline">-- the following expression can appear in assignment context</span>
         <span class="o">|</span> <span class="nc">Attribute</span><span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">value</span>, <span class="bp">identifier</span><span class="o"> </span><span class="n">attr</span>, <span class="bp">expr_context</span><span class="o"> </span><span class="n">ctx</span><span class="p">)</span>
         <span class="o">|</span> <span class="nc">Subscript</span><span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">value</span>, <span class="bp">expr</span><span class="o"> </span><span class="n">slice</span>, <span class="bp">expr_context</span><span class="o"> </span><span class="n">ctx</span><span class="p">)</span>
         <span class="o">|</span> <span class="nc">Starred</span><span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">value</span>, <span class="bp">expr_context</span><span class="o"> </span><span class="n">ctx</span><span class="p">)</span>
         <span class="o">|</span> <span class="nc">Name</span><span class="p">(</span><span class="bp">identifier</span><span class="o"> </span><span class="n">id</span>, <span class="bp">expr_context</span><span class="o"> </span><span class="n">ctx</span><span class="p">)</span>
         <span class="o">|</span> <span class="nc">List</span><span class="p">(</span><span class="bp">expr</span><span class="o">* </span><span class="n">elts</span>, <span class="bp">expr_context</span><span class="o"> </span><span class="n">ctx</span><span class="p">)</span>
         <span class="o">|</span> <span class="nc">Tuple</span><span class="p">(</span><span class="bp">expr</span><span class="o">* </span><span class="n">elts</span>, <span class="bp">expr_context</span><span class="o"> </span><span class="n">ctx</span><span class="p">)</span>

         <span class="c c-Singleline">-- can appear only in Subscript</span>
         <span class="o">|</span> <span class="nc">Slice</span><span class="p">(</span><span class="bp">expr</span><span class="o">? </span><span class="n">lower</span>, <span class="bp">expr</span><span class="o">? </span><span class="n">upper</span>, <span class="bp">expr</span><span class="o">? </span><span class="n">step</span><span class="p">)</span>

          <span class="c c-Singleline">-- col_offset is the byte offset in the utf8 string the parser uses</span>
          <span class="nb">attributes</span> <span class="p">(</span><span class="bp">int</span><span class="o"> </span><span class="n">lineno</span>, <span class="bp">int</span><span class="o"> </span><span class="n">col_offset</span>, <span class="bp">int</span><span class="o">? </span><span class="n">end_lineno</span>, <span class="bp">int</span><span class="o">? </span><span class="n">end_col_offset</span><span class="p">)</span>

    <span class="n">expr_context</span> <span class="o">=</span> <span class="nc">Load</span> <span class="o">|</span> <span class="nc">Store</span> <span class="o">|</span> <span class="nc">Del</span>

    <span class="n">boolop</span> <span class="o">=</span> <span class="nc">And</span> <span class="o">|</span> <span class="nc">Or</span>

    <span class="n">operator</span> <span class="o">=</span> <span class="nc">Add</span> <span class="o">|</span> <span class="nc">Sub</span> <span class="o">|</span> <span class="nc">Mult</span> <span class="o">|</span> <span class="nc">MatMult</span> <span class="o">|</span> <span class="nc">Div</span> <span class="o">|</span> <span class="nc">Mod</span> <span class="o">|</span> <span class="nc">Pow</span> <span class="o">|</span> <span class="nc">LShift</span>
                 <span class="o">|</span> <span class="nc">RShift</span> <span class="o">|</span> <span class="nc">BitOr</span> <span class="o">|</span> <span class="nc">BitXor</span> <span class="o">|</span> <span class="nc">BitAnd</span> <span class="o">|</span> <span class="nc">FloorDiv</span>

    <span class="n">unaryop</span> <span class="o">=</span> <span class="nc">Invert</span> <span class="o">|</span> <span class="nc">Not</span> <span class="o">|</span> <span class="nc">UAdd</span> <span class="o">|</span> <span class="nc">USub</span>

    <span class="n">cmpop</span> <span class="o">=</span> <span class="nc">Eq</span> <span class="o">|</span> <span class="nc">NotEq</span> <span class="o">|</span> <span class="nc">Lt</span> <span class="o">|</span> <span class="nc">LtE</span> <span class="o">|</span> <span class="nc">Gt</span> <span class="o">|</span> <span class="nc">GtE</span> <span class="o">|</span> <span class="nc">Is</span> <span class="o">|</span> <span class="nc">IsNot</span> <span class="o">|</span> <span class="nc">In</span> <span class="o">|</span> <span class="nc">NotIn</span>

    <span class="n">comprehension</span> <span class="o">=</span> <span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">target</span>, <span class="bp">expr</span><span class="o"> </span><span class="n">iter</span>, <span class="bp">expr</span><span class="o">* </span><span class="n">ifs</span>, <span class="bp">int</span><span class="o"> </span><span class="n">is_async</span><span class="p">)</span>

    <span class="n">excepthandler</span> <span class="o">=</span> <span class="nc">ExceptHandler</span><span class="p">(</span><span class="bp">expr</span><span class="o">? </span><span class="n">type</span>, <span class="bp">identifier</span><span class="o">? </span><span class="n">name</span>, <span class="bp">stmt</span><span class="o">* </span><span class="n">body</span><span class="p">)</span>
                    <span class="nb">attributes</span> <span class="p">(</span><span class="bp">int</span><span class="o"> </span><span class="n">lineno</span>, <span class="bp">int</span><span class="o"> </span><span class="n">col_offset</span>, <span class="bp">int</span><span class="o">? </span><span class="n">end_lineno</span>, <span class="bp">int</span><span class="o">? </span><span class="n">end_col_offset</span><span class="p">)</span>

    <span class="n">arguments</span> <span class="o">=</span> <span class="p">(</span><span class="bp">arg</span><span class="o">* </span><span class="n">posonlyargs</span>, <span class="bp">arg</span><span class="o">* </span><span class="n">args</span>, <span class="bp">arg</span><span class="o">? </span><span class="n">vararg</span>, <span class="bp">arg</span><span class="o">* </span><span class="n">kwonlyargs</span>,
                 <span class="bp">expr</span><span class="o">* </span><span class="n">kw_defaults</span>, <span class="bp">arg</span><span class="o">? </span><span class="n">kwarg</span>, <span class="bp">expr</span><span class="o">* </span><span class="n">defaults</span><span class="p">)</span>

    <span class="n">arg</span> <span class="o">=</span> <span class="p">(</span><span class="bp">identifier</span><span class="o"> </span><span class="n">arg</span>, <span class="bp">expr</span><span class="o">? </span><span class="n">annotation</span>, <span class="bp">string</span><span class="o">? </span><span class="n">type_comment</span><span class="p">)</span>
           <span class="nb">attributes</span> <span class="p">(</span><span class="bp">int</span><span class="o"> </span><span class="n">lineno</span>, <span class="bp">int</span><span class="o"> </span><span class="n">col_offset</span>, <span class="bp">int</span><span class="o">? </span><span class="n">end_lineno</span>, <span class="bp">int</span><span class="o">? </span><span class="n">end_col_offset</span><span class="p">)</span>

    <span class="c c-Singleline">-- keyword arguments supplied to call (NULL identifier for **kwargs)</span>
    <span class="n">keyword</span> <span class="o">=</span> <span class="p">(</span><span class="bp">identifier</span><span class="o">? </span><span class="n">arg</span>, <span class="bp">expr</span><span class="o"> </span><span class="n">value</span><span class="p">)</span>
               <span class="nb">attributes</span> <span class="p">(</span><span class="bp">int</span><span class="o"> </span><span class="n">lineno</span>, <span class="bp">int</span><span class="o"> </span><span class="n">col_offset</span>, <span class="bp">int</span><span class="o">? </span><span class="n">end_lineno</span>, <span class="bp">int</span><span class="o">? </span><span class="n">end_col_offset</span><span class="p">)</span>

    <span class="c c-Singleline">-- import name with optional &#39;as&#39; alias.</span>
    <span class="n">alias</span> <span class="o">=</span> <span class="p">(</span><span class="bp">identifier</span><span class="o"> </span><span class="n">name</span>, <span class="bp">identifier</span><span class="o">? </span><span class="n">asname</span><span class="p">)</span>

    <span class="n">withitem</span> <span class="o">=</span> <span class="p">(</span><span class="bp">expr</span><span class="o"> </span><span class="n">context_expr</span>, <span class="bp">expr</span><span class="o">? </span><span class="n">optional_vars</span><span class="p">)</span>

    <span class="n">type_ignore</span> <span class="o">=</span> <span class="nc">TypeIgnore</span><span class="p">(</span><span class="bp">int</span><span class="o"> </span><span class="n">lineno</span>, <span class="bp">string</span><span class="o"> </span><span class="n">tag</span><span class="p">)</span>
<span class="p">}</span>
</pre></div>
</div>
</section>
<section id="node-classes">
<h2>Node classes<a class="headerlink" href="#node-classes" title="Permalink to this headline">¶</a></h2>
<dl class="class">
<dt id="ast.AST">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">AST</code><a class="headerlink" href="#ast.AST" title="Permalink to this definition">¶</a></dt>
<dd><p>This is the base of all AST node classes.  The actual node classes are
derived from the <code class="file docutils literal notranslate"><span class="pre">Parser/Python.asdl</span></code> file, which is reproduced
<a class="reference internal" href="#abstract-grammar"><span class="std std-ref">below</span></a>.  They are defined in the <code class="xref py py-mod docutils literal notranslate"><span class="pre">_ast</span></code> C
module and re-exported in <a class="reference internal" href="#module-ast" title="ast: Abstract Syntax Tree classes and manipulation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ast</span></code></a>.</p>
<p>There is one class defined for each left-hand side symbol in the abstract
grammar (for example, <code class="xref py py-class docutils literal notranslate"><span class="pre">ast.stmt</span></code> or <code class="xref py py-class docutils literal notranslate"><span class="pre">ast.expr</span></code>).  In addition,
there is one class defined for each constructor on the right-hand side; these
classes inherit from the classes for the left-hand side trees.  For example,
<a class="reference internal" href="#ast.BinOp" title="ast.BinOp"><code class="xref py py-class docutils literal notranslate"><span class="pre">ast.BinOp</span></code></a> inherits from <code class="xref py py-class docutils literal notranslate"><span class="pre">ast.expr</span></code>.  For production rules
with alternatives (aka “sums”), the left-hand side class is abstract: only
instances of specific constructor nodes are ever created.</p>
<span class="target" id="index-0"></span><span class="target" id="index-1"></span><dl class="attribute">
<dt id="ast.AST._fields">
<code class="sig-name descname">_fields</code><a class="headerlink" href="#ast.AST._fields" title="Permalink to this definition">¶</a></dt>
<dd><p>Each concrete class has an attribute <a class="reference internal" href="#ast.AST._fields" title="ast.AST._fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">_fields</span></code></a> which gives the names
of all child nodes.</p>
<p>Each instance of a concrete class has one attribute for each child node,
of the type as defined in the grammar.  For example, <a class="reference internal" href="#ast.BinOp" title="ast.BinOp"><code class="xref py py-class docutils literal notranslate"><span class="pre">ast.BinOp</span></code></a>
instances have an attribute <code class="xref py py-attr docutils literal notranslate"><span class="pre">left</span></code> of type <code class="xref py py-class docutils literal notranslate"><span class="pre">ast.expr</span></code>.</p>
<p>If these attributes are marked as optional in the grammar (using a
question mark), the value might be <code class="docutils literal notranslate"><span class="pre">None</span></code>.  If the attributes can have
zero-or-more values (marked with an asterisk), the values are represented
as Python lists.  All possible attributes must be present and have valid
values when compiling an AST with <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a>.</p>
</dd></dl>

<dl class="attribute">
<dt id="ast.AST.lineno">
<code class="sig-name descname">lineno</code><a class="headerlink" href="#ast.AST.lineno" title="Permalink to this definition">¶</a></dt>
<dt id="ast.AST.col_offset">
<code class="sig-name descname">col_offset</code><a class="headerlink" href="#ast.AST.col_offset" title="Permalink to this definition">¶</a></dt>
<dt id="ast.AST.end_lineno">
<code class="sig-name descname">end_lineno</code><a class="headerlink" href="#ast.AST.end_lineno" title="Permalink to this definition">¶</a></dt>
<dt id="ast.AST.end_col_offset">
<code class="sig-name descname">end_col_offset</code><a class="headerlink" href="#ast.AST.end_col_offset" title="Permalink to this definition">¶</a></dt>
<dd><p>Instances of <code class="xref py py-class docutils literal notranslate"><span class="pre">ast.expr</span></code> and <code class="xref py py-class docutils literal notranslate"><span class="pre">ast.stmt</span></code> subclasses have
<a class="reference internal" href="#ast.AST.lineno" title="ast.AST.lineno"><code class="xref py py-attr docutils literal notranslate"><span class="pre">lineno</span></code></a>, <a class="reference internal" href="#ast.AST.col_offset" title="ast.AST.col_offset"><code class="xref py py-attr docutils literal notranslate"><span class="pre">col_offset</span></code></a>, <a class="reference internal" href="#ast.AST.end_lineno" title="ast.AST.end_lineno"><code class="xref py py-attr docutils literal notranslate"><span class="pre">end_lineno</span></code></a>, and
<a class="reference internal" href="#ast.AST.end_col_offset" title="ast.AST.end_col_offset"><code class="xref py py-attr docutils literal notranslate"><span class="pre">end_col_offset</span></code></a> attributes.  The <a class="reference internal" href="#ast.AST.lineno" title="ast.AST.lineno"><code class="xref py py-attr docutils literal notranslate"><span class="pre">lineno</span></code></a> and
<a class="reference internal" href="#ast.AST.end_lineno" title="ast.AST.end_lineno"><code class="xref py py-attr docutils literal notranslate"><span class="pre">end_lineno</span></code></a> are the first and last line numbers of the source
text span (1-indexed so the first line is line 1), and the
<a class="reference internal" href="#ast.AST.col_offset" title="ast.AST.col_offset"><code class="xref py py-attr docutils literal notranslate"><span class="pre">col_offset</span></code></a> and <a class="reference internal" href="#ast.AST.end_col_offset" title="ast.AST.end_col_offset"><code class="xref py py-attr docutils literal notranslate"><span class="pre">end_col_offset</span></code></a> are the corresponding
UTF-8 byte offsets of the first and last tokens that generated the node.
The UTF-8 offset is recorded because the parser uses UTF-8 internally.</p>
<p>Note that the end positions are not required by the compiler and are
therefore optional. The end offset is <em>after</em> the last symbol, for example
one can get the source segment of a one-line expression node using
<code class="docutils literal notranslate"><span class="pre">source_line[node.col_offset</span> <span class="pre">:</span> <span class="pre">node.end_col_offset]</span></code>.</p>
</dd></dl>

<p>The constructor of a class <code class="xref py py-class docutils literal notranslate"><span class="pre">ast.T</span></code> parses its arguments as follows:</p>
<ul class="simple">
<li><p>If there are positional arguments, there must be as many as there are items
in <code class="xref py py-attr docutils literal notranslate"><span class="pre">T._fields</span></code>; they will be assigned as attributes of these names.</p></li>
<li><p>If there are keyword arguments, they will set the attributes of the same
names to the given values.</p></li>
</ul>
<p>For example, to create and populate an <a class="reference internal" href="#ast.UnaryOp" title="ast.UnaryOp"><code class="xref py py-class docutils literal notranslate"><span class="pre">ast.UnaryOp</span></code></a> node, you could
use</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">node</span> <span class="o">=</span> <span class="n">ast</span><span class="o">.</span><span class="n">UnaryOp</span><span class="p">()</span>
<span class="n">node</span><span class="o">.</span><span class="n">op</span> <span class="o">=</span> <span class="n">ast</span><span class="o">.</span><span class="n">USub</span><span class="p">()</span>
<span class="n">node</span><span class="o">.</span><span class="n">operand</span> <span class="o">=</span> <span class="n">ast</span><span class="o">.</span><span class="n">Constant</span><span class="p">()</span>
<span class="n">node</span><span class="o">.</span><span class="n">operand</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="mi">5</span>
<span class="n">node</span><span class="o">.</span><span class="n">operand</span><span class="o">.</span><span class="n">lineno</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">node</span><span class="o">.</span><span class="n">operand</span><span class="o">.</span><span class="n">col_offset</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">node</span><span class="o">.</span><span class="n">lineno</span> <span class="o">=</span> <span class="mi">0</span>
<span class="n">node</span><span class="o">.</span><span class="n">col_offset</span> <span class="o">=</span> <span class="mi">0</span>
</pre></div>
</div>
<p>or the more compact</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">node</span> <span class="o">=</span> <span class="n">ast</span><span class="o">.</span><span class="n">UnaryOp</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">USub</span><span class="p">(),</span> <span class="n">ast</span><span class="o">.</span><span class="n">Constant</span><span class="p">(</span><span class="mi">5</span><span class="p">,</span> <span class="n">lineno</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">col_offset</span><span class="o">=</span><span class="mi">0</span><span class="p">),</span>
                   <span class="n">lineno</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">col_offset</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.8: </span>Class <a class="reference internal" href="#ast.Constant" title="ast.Constant"><code class="xref py py-class docutils literal notranslate"><span class="pre">ast.Constant</span></code></a> is now used for all constants.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.9: </span>Simple indices are represented by their value, extended slices are
represented as tuples.</p>
</div>
<div class="deprecated">
<p><span class="versionmodified deprecated">Deprecated since version 3.8: </span>Old classes <code class="xref py py-class docutils literal notranslate"><span class="pre">ast.Num</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">ast.Str</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">ast.Bytes</span></code>,
<code class="xref py py-class docutils literal notranslate"><span class="pre">ast.NameConstant</span></code> and <code class="xref py py-class docutils literal notranslate"><span class="pre">ast.Ellipsis</span></code> are still available,
but they will be removed in future Python releases.  In the meantime,
instantiating them will return an instance of a different class.</p>
</div>
<div class="deprecated">
<p><span class="versionmodified deprecated">Deprecated since version 3.9: </span>Old classes <code class="xref py py-class docutils literal notranslate"><span class="pre">ast.Index</span></code> and <code class="xref py py-class docutils literal notranslate"><span class="pre">ast.ExtSlice</span></code> are still
available, but they will be removed in future Python releases.
In the meantime, instantiating them will return an instance of
a different class.</p>
</div>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The descriptions of the specific node classes displayed here
were initially adapted from the fantastic <a class="reference external" href="https://greentreesnakes.readthedocs.io/en/latest/">Green Tree
Snakes</a> project and
all its contributors.</p>
</div>
<section id="literals">
<h3>Literals<a class="headerlink" href="#literals" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="ast.Constant">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Constant</code><span class="sig-paren">(</span><em class="sig-param">value</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.Constant" title="Permalink to this definition">¶</a></dt>
<dd><p>A constant value. The <code class="docutils literal notranslate"><span class="pre">value</span></code> attribute of the <code class="docutils literal notranslate"><span class="pre">Constant</span></code> literal contains the
Python object it represents. The values represented can be simple types
such as a number, string or <code class="docutils literal notranslate"><span class="pre">None</span></code>, but also immutable container types
(tuples and frozensets) if all of their elements are constant.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;123&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;eval&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Expression(</span>
<span class="go">    body=Constant(value=123))</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.FormattedValue">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">FormattedValue</code><span class="sig-paren">(</span><em class="sig-param">value</em>, <em class="sig-param">conversion</em>, <em class="sig-param">format_spec</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.FormattedValue" title="Permalink to this definition">¶</a></dt>
<dd><p>Node representing a single formatting field in an f-string. If the string
contains a single formatting field and nothing else the node can be
isolated otherwise it appears in <a class="reference internal" href="#ast.JoinedStr" title="ast.JoinedStr"><code class="xref py py-class docutils literal notranslate"><span class="pre">JoinedStr</span></code></a>.</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">value</span></code> is any expression node (such as a literal, a variable, or a
function call).</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">conversion</span></code> is an integer:</p>
<ul>
<li><p>-1: no formatting</p></li>
<li><p>115: <code class="docutils literal notranslate"><span class="pre">!s</span></code> string formatting</p></li>
<li><p>114: <code class="docutils literal notranslate"><span class="pre">!r</span></code> repr formatting</p></li>
<li><p>97: <code class="docutils literal notranslate"><span class="pre">!a</span></code> ascii formatting</p></li>
</ul>
</li>
<li><p><code class="docutils literal notranslate"><span class="pre">format_spec</span></code> is a <a class="reference internal" href="#ast.JoinedStr" title="ast.JoinedStr"><code class="xref py py-class docutils literal notranslate"><span class="pre">JoinedStr</span></code></a> node representing the formatting
of the value, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if no format was specified. Both
<code class="docutils literal notranslate"><span class="pre">conversion</span></code> and <code class="docutils literal notranslate"><span class="pre">format_spec</span></code> can be set at the same time.</p></li>
</ul>
</dd></dl>

<dl class="class">
<dt id="ast.JoinedStr">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">JoinedStr</code><span class="sig-paren">(</span><em class="sig-param">values</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.JoinedStr" title="Permalink to this definition">¶</a></dt>
<dd><p>An f-string, comprising a series of <a class="reference internal" href="#ast.FormattedValue" title="ast.FormattedValue"><code class="xref py py-class docutils literal notranslate"><span class="pre">FormattedValue</span></code></a> and <a class="reference internal" href="#ast.Constant" title="ast.Constant"><code class="xref py py-class docutils literal notranslate"><span class="pre">Constant</span></code></a>
nodes.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;f&quot;sin(</span><span class="si">{a}</span><span class="s1">) is {sin(a):.3}&quot;&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;eval&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Expression(</span>
<span class="go">    body=JoinedStr(</span>
<span class="go">        values=[</span>
<span class="go">            Constant(value=&#39;sin(&#39;),</span>
<span class="go">            FormattedValue(</span>
<span class="go">                value=Name(id=&#39;a&#39;, ctx=Load()),</span>
<span class="go">                conversion=-1),</span>
<span class="go">            Constant(value=&#39;) is &#39;),</span>
<span class="go">            FormattedValue(</span>
<span class="go">                value=Call(</span>
<span class="go">                    func=Name(id=&#39;sin&#39;, ctx=Load()),</span>
<span class="go">                    args=[</span>
<span class="go">                        Name(id=&#39;a&#39;, ctx=Load())],</span>
<span class="go">                    keywords=[]),</span>
<span class="go">                conversion=-1,</span>
<span class="go">                format_spec=JoinedStr(</span>
<span class="go">                    values=[</span>
<span class="go">                        Constant(value=&#39;.3&#39;)]))]))</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.List">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">List</code><span class="sig-paren">(</span><em class="sig-param">elts</em>, <em class="sig-param">ctx</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.List" title="Permalink to this definition">¶</a></dt>
<dt id="ast.Tuple">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Tuple</code><span class="sig-paren">(</span><em class="sig-param">elts</em>, <em class="sig-param">ctx</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.Tuple" title="Permalink to this definition">¶</a></dt>
<dd><p>A list or tuple. <code class="docutils literal notranslate"><span class="pre">elts</span></code> holds a list of nodes representing the elements.
<code class="docutils literal notranslate"><span class="pre">ctx</span></code> is <a class="reference internal" href="#ast.Store" title="ast.Store"><code class="xref py py-class docutils literal notranslate"><span class="pre">Store</span></code></a> if the container is an assignment target (i.e.
<code class="docutils literal notranslate"><span class="pre">(x,y)=something</span></code>), and <a class="reference internal" href="#ast.Load" title="ast.Load"><code class="xref py py-class docutils literal notranslate"><span class="pre">Load</span></code></a> otherwise.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;[1, 2, 3]&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;eval&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Expression(</span>
<span class="go">    body=List(</span>
<span class="go">        elts=[</span>
<span class="go">            Constant(value=1),</span>
<span class="go">            Constant(value=2),</span>
<span class="go">            Constant(value=3)],</span>
<span class="go">        ctx=Load()))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;(1, 2, 3)&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;eval&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Expression(</span>
<span class="go">    body=Tuple(</span>
<span class="go">        elts=[</span>
<span class="go">            Constant(value=1),</span>
<span class="go">            Constant(value=2),</span>
<span class="go">            Constant(value=3)],</span>
<span class="go">        ctx=Load()))</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.Set">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Set</code><span class="sig-paren">(</span><em class="sig-param">elts</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.Set" title="Permalink to this definition">¶</a></dt>
<dd><p>A set. <code class="docutils literal notranslate"><span class="pre">elts</span></code> holds a list of nodes representing the set’s elements.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;{1, 2, 3}&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;eval&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Expression(</span>
<span class="go">    body=Set(</span>
<span class="go">        elts=[</span>
<span class="go">            Constant(value=1),</span>
<span class="go">            Constant(value=2),</span>
<span class="go">            Constant(value=3)]))</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.Dict">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Dict</code><span class="sig-paren">(</span><em class="sig-param">keys</em>, <em class="sig-param">values</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.Dict" title="Permalink to this definition">¶</a></dt>
<dd><p>A dictionary. <code class="docutils literal notranslate"><span class="pre">keys</span></code> and <code class="docutils literal notranslate"><span class="pre">values</span></code> hold lists of nodes representing the
keys and the values respectively, in matching order (what would be returned
when calling <code class="code docutils literal notranslate"><span class="pre">dictionary.keys()</span></code> and <code class="code docutils literal notranslate"><span class="pre">dictionary.values()</span></code>).</p>
<p>When doing dictionary unpacking using dictionary literals the expression to be
expanded goes in the <code class="docutils literal notranslate"><span class="pre">values</span></code> list, with a <code class="docutils literal notranslate"><span class="pre">None</span></code> at the corresponding
position in <code class="docutils literal notranslate"><span class="pre">keys</span></code>.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;{&quot;a&quot;:1, **d}&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;eval&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Expression(</span>
<span class="go">    body=Dict(</span>
<span class="go">        keys=[</span>
<span class="go">            Constant(value=&#39;a&#39;),</span>
<span class="go">            None],</span>
<span class="go">        values=[</span>
<span class="go">            Constant(value=1),</span>
<span class="go">            Name(id=&#39;d&#39;, ctx=Load())]))</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="variables">
<h3>Variables<a class="headerlink" href="#variables" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="ast.Name">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Name</code><span class="sig-paren">(</span><em class="sig-param">id</em>, <em class="sig-param">ctx</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.Name" title="Permalink to this definition">¶</a></dt>
<dd><p>A variable name. <code class="docutils literal notranslate"><span class="pre">id</span></code> holds the name as a string, and <code class="docutils literal notranslate"><span class="pre">ctx</span></code> is one of
the following types.</p>
</dd></dl>

<dl class="class">
<dt id="ast.Load">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Load</code><a class="headerlink" href="#ast.Load" title="Permalink to this definition">¶</a></dt>
<dt id="ast.Store">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Store</code><a class="headerlink" href="#ast.Store" title="Permalink to this definition">¶</a></dt>
<dt id="ast.Del">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Del</code><a class="headerlink" href="#ast.Del" title="Permalink to this definition">¶</a></dt>
<dd><p>Variable references can be used to load the value of a variable, to assign
a new value to it, or to delete it. Variable references are given a context
to distinguish these cases.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;a&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        Expr(</span>
<span class="go">            value=Name(id=&#39;a&#39;, ctx=Load()))],</span>
<span class="go">    type_ignores=[])</span>

<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;a = 1&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        Assign(</span>
<span class="go">            targets=[</span>
<span class="go">                Name(id=&#39;a&#39;, ctx=Store())],</span>
<span class="go">            value=Constant(value=1))],</span>
<span class="go">    type_ignores=[])</span>

<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;del a&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        Delete(</span>
<span class="go">            targets=[</span>
<span class="go">                Name(id=&#39;a&#39;, ctx=Del())])],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.Starred">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Starred</code><span class="sig-paren">(</span><em class="sig-param">value</em>, <em class="sig-param">ctx</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.Starred" title="Permalink to this definition">¶</a></dt>
<dd><p>A <code class="docutils literal notranslate"><span class="pre">*var</span></code> variable reference. <code class="docutils literal notranslate"><span class="pre">value</span></code> holds the variable, typically a
<a class="reference internal" href="#ast.Name" title="ast.Name"><code class="xref py py-class docutils literal notranslate"><span class="pre">Name</span></code></a> node. This type must be used when building a <a class="reference internal" href="#ast.Call" title="ast.Call"><code class="xref py py-class docutils literal notranslate"><span class="pre">Call</span></code></a>
node with <code class="docutils literal notranslate"><span class="pre">*args</span></code>.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;a, *b = it&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        Assign(</span>
<span class="go">            targets=[</span>
<span class="go">                Tuple(</span>
<span class="go">                    elts=[</span>
<span class="go">                        Name(id=&#39;a&#39;, ctx=Store()),</span>
<span class="go">                        Starred(</span>
<span class="go">                            value=Name(id=&#39;b&#39;, ctx=Store()),</span>
<span class="go">                            ctx=Store())],</span>
<span class="go">                    ctx=Store())],</span>
<span class="go">            value=Name(id=&#39;it&#39;, ctx=Load()))],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="expressions">
<h3>Expressions<a class="headerlink" href="#expressions" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="ast.Expr">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Expr</code><span class="sig-paren">(</span><em class="sig-param">value</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.Expr" title="Permalink to this definition">¶</a></dt>
<dd><p>When an expression, such as a function call, appears as a statement by itself
with its return value not used or stored, it is wrapped in this container.
<code class="docutils literal notranslate"><span class="pre">value</span></code> holds one of the other nodes in this section, a <a class="reference internal" href="#ast.Constant" title="ast.Constant"><code class="xref py py-class docutils literal notranslate"><span class="pre">Constant</span></code></a>, a
<a class="reference internal" href="#ast.Name" title="ast.Name"><code class="xref py py-class docutils literal notranslate"><span class="pre">Name</span></code></a>, a <a class="reference internal" href="#ast.Lambda" title="ast.Lambda"><code class="xref py py-class docutils literal notranslate"><span class="pre">Lambda</span></code></a>, a <a class="reference internal" href="#ast.Yield" title="ast.Yield"><code class="xref py py-class docutils literal notranslate"><span class="pre">Yield</span></code></a> or <a class="reference internal" href="#ast.YieldFrom" title="ast.YieldFrom"><code class="xref py py-class docutils literal notranslate"><span class="pre">YieldFrom</span></code></a> node.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;-a&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        Expr(</span>
<span class="go">            value=UnaryOp(</span>
<span class="go">                op=USub(),</span>
<span class="go">                operand=Name(id=&#39;a&#39;, ctx=Load())))],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.UnaryOp">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">UnaryOp</code><span class="sig-paren">(</span><em class="sig-param">op</em>, <em class="sig-param">operand</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.UnaryOp" title="Permalink to this definition">¶</a></dt>
<dd><p>A unary operation. <code class="docutils literal notranslate"><span class="pre">op</span></code> is the operator, and <code class="docutils literal notranslate"><span class="pre">operand</span></code> any expression
node.</p>
</dd></dl>

<dl class="class">
<dt id="ast.UAdd">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">UAdd</code><a class="headerlink" href="#ast.UAdd" title="Permalink to this definition">¶</a></dt>
<dt id="ast.USub">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">USub</code><a class="headerlink" href="#ast.USub" title="Permalink to this definition">¶</a></dt>
<dt id="ast.Not">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Not</code><a class="headerlink" href="#ast.Not" title="Permalink to this definition">¶</a></dt>
<dt id="ast.Invert">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Invert</code><a class="headerlink" href="#ast.Invert" title="Permalink to this definition">¶</a></dt>
<dd><p>Unary operator tokens. <a class="reference internal" href="#ast.Not" title="ast.Not"><code class="xref py py-class docutils literal notranslate"><span class="pre">Not</span></code></a> is the <code class="docutils literal notranslate"><span class="pre">not</span></code> keyword, <a class="reference internal" href="#ast.Invert" title="ast.Invert"><code class="xref py py-class docutils literal notranslate"><span class="pre">Invert</span></code></a>
is the <code class="docutils literal notranslate"><span class="pre">~</span></code> operator.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;not x&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;eval&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Expression(</span>
<span class="go">    body=UnaryOp(</span>
<span class="go">        op=Not(),</span>
<span class="go">        operand=Name(id=&#39;x&#39;, ctx=Load())))</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.BinOp">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">BinOp</code><span class="sig-paren">(</span><em class="sig-param">left</em>, <em class="sig-param">op</em>, <em class="sig-param">right</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.BinOp" title="Permalink to this definition">¶</a></dt>
<dd><p>A binary operation (like addition or division). <code class="docutils literal notranslate"><span class="pre">op</span></code> is the operator, and
<code class="docutils literal notranslate"><span class="pre">left</span></code> and <code class="docutils literal notranslate"><span class="pre">right</span></code> are any expression nodes.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;x + y&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;eval&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Expression(</span>
<span class="go">    body=BinOp(</span>
<span class="go">        left=Name(id=&#39;x&#39;, ctx=Load()),</span>
<span class="go">        op=Add(),</span>
<span class="go">        right=Name(id=&#39;y&#39;, ctx=Load())))</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.Add">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Add</code><a class="headerlink" href="#ast.Add" title="Permalink to this definition">¶</a></dt>
<dt id="ast.Sub">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Sub</code><a class="headerlink" href="#ast.Sub" title="Permalink to this definition">¶</a></dt>
<dt id="ast.Mult">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Mult</code><a class="headerlink" href="#ast.Mult" title="Permalink to this definition">¶</a></dt>
<dt id="ast.Div">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Div</code><a class="headerlink" href="#ast.Div" title="Permalink to this definition">¶</a></dt>
<dt id="ast.FloorDiv">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">FloorDiv</code><a class="headerlink" href="#ast.FloorDiv" title="Permalink to this definition">¶</a></dt>
<dt id="ast.Mod">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Mod</code><a class="headerlink" href="#ast.Mod" title="Permalink to this definition">¶</a></dt>
<dt id="ast.Pow">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Pow</code><a class="headerlink" href="#ast.Pow" title="Permalink to this definition">¶</a></dt>
<dt id="ast.LShift">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">LShift</code><a class="headerlink" href="#ast.LShift" title="Permalink to this definition">¶</a></dt>
<dt id="ast.RShift">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">RShift</code><a class="headerlink" href="#ast.RShift" title="Permalink to this definition">¶</a></dt>
<dt id="ast.BitOr">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">BitOr</code><a class="headerlink" href="#ast.BitOr" title="Permalink to this definition">¶</a></dt>
<dt id="ast.BitXor">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">BitXor</code><a class="headerlink" href="#ast.BitXor" title="Permalink to this definition">¶</a></dt>
<dt id="ast.BitAnd">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">BitAnd</code><a class="headerlink" href="#ast.BitAnd" title="Permalink to this definition">¶</a></dt>
<dt id="ast.MatMult">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">MatMult</code><a class="headerlink" href="#ast.MatMult" title="Permalink to this definition">¶</a></dt>
<dd><p>Binary operator tokens.</p>
</dd></dl>

<dl class="class">
<dt id="ast.BoolOp">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">BoolOp</code><span class="sig-paren">(</span><em class="sig-param">op</em>, <em class="sig-param">values</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.BoolOp" title="Permalink to this definition">¶</a></dt>
<dd><p>A boolean operation, ‘or’ or ‘and’. <code class="docutils literal notranslate"><span class="pre">op</span></code> is <a class="reference internal" href="#ast.Or" title="ast.Or"><code class="xref py py-class docutils literal notranslate"><span class="pre">Or</span></code></a> or <a class="reference internal" href="#ast.And" title="ast.And"><code class="xref py py-class docutils literal notranslate"><span class="pre">And</span></code></a>.
<code class="docutils literal notranslate"><span class="pre">values</span></code> are the values involved. Consecutive operations with the same
operator, such as <code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">or</span> <span class="pre">b</span> <span class="pre">or</span> <span class="pre">c</span></code>, are collapsed into one node with several
values.</p>
<p>This doesn’t include <code class="docutils literal notranslate"><span class="pre">not</span></code>, which is a <a class="reference internal" href="#ast.UnaryOp" title="ast.UnaryOp"><code class="xref py py-class docutils literal notranslate"><span class="pre">UnaryOp</span></code></a>.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;x or y&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;eval&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Expression(</span>
<span class="go">    body=BoolOp(</span>
<span class="go">        op=Or(),</span>
<span class="go">        values=[</span>
<span class="go">            Name(id=&#39;x&#39;, ctx=Load()),</span>
<span class="go">            Name(id=&#39;y&#39;, ctx=Load())]))</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.And">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">And</code><a class="headerlink" href="#ast.And" title="Permalink to this definition">¶</a></dt>
<dt id="ast.Or">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Or</code><a class="headerlink" href="#ast.Or" title="Permalink to this definition">¶</a></dt>
<dd><p>Boolean operator tokens.</p>
</dd></dl>

<dl class="class">
<dt id="ast.Compare">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Compare</code><span class="sig-paren">(</span><em class="sig-param">left</em>, <em class="sig-param">ops</em>, <em class="sig-param">comparators</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.Compare" title="Permalink to this definition">¶</a></dt>
<dd><p>A comparison of two or more values. <code class="docutils literal notranslate"><span class="pre">left</span></code> is the first value in the
comparison, <code class="docutils literal notranslate"><span class="pre">ops</span></code> the list of operators, and <code class="docutils literal notranslate"><span class="pre">comparators</span></code> the list
of values after the first element in the comparison.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;1 &lt;= a &lt; 10&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;eval&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Expression(</span>
<span class="go">    body=Compare(</span>
<span class="go">        left=Constant(value=1),</span>
<span class="go">        ops=[</span>
<span class="go">            LtE(),</span>
<span class="go">            Lt()],</span>
<span class="go">        comparators=[</span>
<span class="go">            Name(id=&#39;a&#39;, ctx=Load()),</span>
<span class="go">            Constant(value=10)]))</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.Eq">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Eq</code><a class="headerlink" href="#ast.Eq" title="Permalink to this definition">¶</a></dt>
<dt id="ast.NotEq">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">NotEq</code><a class="headerlink" href="#ast.NotEq" title="Permalink to this definition">¶</a></dt>
<dt id="ast.Lt">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Lt</code><a class="headerlink" href="#ast.Lt" title="Permalink to this definition">¶</a></dt>
<dt id="ast.LtE">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">LtE</code><a class="headerlink" href="#ast.LtE" title="Permalink to this definition">¶</a></dt>
<dt id="ast.Gt">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Gt</code><a class="headerlink" href="#ast.Gt" title="Permalink to this definition">¶</a></dt>
<dt id="ast.GtE">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">GtE</code><a class="headerlink" href="#ast.GtE" title="Permalink to this definition">¶</a></dt>
<dt id="ast.Is">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Is</code><a class="headerlink" href="#ast.Is" title="Permalink to this definition">¶</a></dt>
<dt id="ast.IsNot">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">IsNot</code><a class="headerlink" href="#ast.IsNot" title="Permalink to this definition">¶</a></dt>
<dt id="ast.In">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">In</code><a class="headerlink" href="#ast.In" title="Permalink to this definition">¶</a></dt>
<dt id="ast.NotIn">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">NotIn</code><a class="headerlink" href="#ast.NotIn" title="Permalink to this definition">¶</a></dt>
<dd><p>Comparison operator tokens.</p>
</dd></dl>

<dl class="class">
<dt id="ast.Call">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Call</code><span class="sig-paren">(</span><em class="sig-param">func</em>, <em class="sig-param">args</em>, <em class="sig-param">keywords</em>, <em class="sig-param">starargs</em>, <em class="sig-param">kwargs</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.Call" title="Permalink to this definition">¶</a></dt>
<dd><p>A function call. <code class="docutils literal notranslate"><span class="pre">func</span></code> is the function, which will often be a
<a class="reference internal" href="#ast.Name" title="ast.Name"><code class="xref py py-class docutils literal notranslate"><span class="pre">Name</span></code></a> or <a class="reference internal" href="#ast.Attribute" title="ast.Attribute"><code class="xref py py-class docutils literal notranslate"><span class="pre">Attribute</span></code></a> object. Of the arguments:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">args</span></code> holds a list of the arguments passed by position.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">keywords</span></code> holds a list of <a class="reference internal" href="keyword.html#module-keyword" title="keyword: Test whether a string is a keyword in Python."><code class="xref py py-class docutils literal notranslate"><span class="pre">keyword</span></code></a> objects representing
arguments passed by keyword.</p></li>
</ul>
<p>When creating a <code class="docutils literal notranslate"><span class="pre">Call</span></code> node, <code class="docutils literal notranslate"><span class="pre">args</span></code> and <code class="docutils literal notranslate"><span class="pre">keywords</span></code> are required, but
they can be empty lists. <code class="docutils literal notranslate"><span class="pre">starargs</span></code> and <code class="docutils literal notranslate"><span class="pre">kwargs</span></code> are optional.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;func(a, b=c, *d, **e)&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;eval&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Expression(</span>
<span class="go">    body=Call(</span>
<span class="go">        func=Name(id=&#39;func&#39;, ctx=Load()),</span>
<span class="go">        args=[</span>
<span class="go">            Name(id=&#39;a&#39;, ctx=Load()),</span>
<span class="go">            Starred(</span>
<span class="go">                value=Name(id=&#39;d&#39;, ctx=Load()),</span>
<span class="go">                ctx=Load())],</span>
<span class="go">        keywords=[</span>
<span class="go">            keyword(</span>
<span class="go">                arg=&#39;b&#39;,</span>
<span class="go">                value=Name(id=&#39;c&#39;, ctx=Load())),</span>
<span class="go">            keyword(</span>
<span class="go">                value=Name(id=&#39;e&#39;, ctx=Load()))]))</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.keyword">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">keyword</code><span class="sig-paren">(</span><em class="sig-param">arg</em>, <em class="sig-param">value</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.keyword" title="Permalink to this definition">¶</a></dt>
<dd><p>A keyword argument to a function call or class definition. <code class="docutils literal notranslate"><span class="pre">arg</span></code> is a raw
string of the parameter name, <code class="docutils literal notranslate"><span class="pre">value</span></code> is a node to pass in.</p>
</dd></dl>

<dl class="class">
<dt id="ast.IfExp">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">IfExp</code><span class="sig-paren">(</span><em class="sig-param">test</em>, <em class="sig-param">body</em>, <em class="sig-param">orelse</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.IfExp" title="Permalink to this definition">¶</a></dt>
<dd><p>An expression such as <code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">if</span> <span class="pre">b</span> <span class="pre">else</span> <span class="pre">c</span></code>. Each field holds a single node, so
in the following example, all three are <a class="reference internal" href="#ast.Name" title="ast.Name"><code class="xref py py-class docutils literal notranslate"><span class="pre">Name</span></code></a> nodes.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;a if b else c&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;eval&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Expression(</span>
<span class="go">    body=IfExp(</span>
<span class="go">        test=Name(id=&#39;b&#39;, ctx=Load()),</span>
<span class="go">        body=Name(id=&#39;a&#39;, ctx=Load()),</span>
<span class="go">        orelse=Name(id=&#39;c&#39;, ctx=Load())))</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.Attribute">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Attribute</code><span class="sig-paren">(</span><em class="sig-param">value</em>, <em class="sig-param">attr</em>, <em class="sig-param">ctx</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.Attribute" title="Permalink to this definition">¶</a></dt>
<dd><p>Attribute access, e.g. <code class="docutils literal notranslate"><span class="pre">d.keys</span></code>. <code class="docutils literal notranslate"><span class="pre">value</span></code> is a node, typically a
<a class="reference internal" href="#ast.Name" title="ast.Name"><code class="xref py py-class docutils literal notranslate"><span class="pre">Name</span></code></a>. <code class="docutils literal notranslate"><span class="pre">attr</span></code> is a bare string giving the name of the attribute,
and <code class="docutils literal notranslate"><span class="pre">ctx</span></code> is <a class="reference internal" href="#ast.Load" title="ast.Load"><code class="xref py py-class docutils literal notranslate"><span class="pre">Load</span></code></a>, <a class="reference internal" href="#ast.Store" title="ast.Store"><code class="xref py py-class docutils literal notranslate"><span class="pre">Store</span></code></a> or <a class="reference internal" href="#ast.Del" title="ast.Del"><code class="xref py py-class docutils literal notranslate"><span class="pre">Del</span></code></a> according to how
the attribute is acted on.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;snake.colour&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;eval&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Expression(</span>
<span class="go">    body=Attribute(</span>
<span class="go">        value=Name(id=&#39;snake&#39;, ctx=Load()),</span>
<span class="go">        attr=&#39;colour&#39;,</span>
<span class="go">        ctx=Load()))</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.NamedExpr">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">NamedExpr</code><span class="sig-paren">(</span><em class="sig-param">target</em>, <em class="sig-param">value</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.NamedExpr" title="Permalink to this definition">¶</a></dt>
<dd><blockquote>
<div><p>A named expression. This AST node is produced by the assignment expressions
operator (also known as the walrus operator). As opposed to the <a class="reference internal" href="#ast.Assign" title="ast.Assign"><code class="xref py py-class docutils literal notranslate"><span class="pre">Assign</span></code></a>
node in which the first argument can be multiple nodes, in this case both
<code class="docutils literal notranslate"><span class="pre">target</span></code> and <code class="docutils literal notranslate"><span class="pre">value</span></code> must be single nodes.</p>
</div></blockquote>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;(x := 4)&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;eval&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Expression(</span>
<span class="go">    body=NamedExpr(</span>
<span class="go">        target=Name(id=&#39;x&#39;, ctx=Store()),</span>
<span class="go">        value=Constant(value=4)))</span>
</pre></div>
</div>
</dd></dl>

<section id="subscripting">
<h4>Subscripting<a class="headerlink" href="#subscripting" title="Permalink to this headline">¶</a></h4>
<dl class="class">
<dt id="ast.Subscript">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Subscript</code><span class="sig-paren">(</span><em class="sig-param">value</em>, <em class="sig-param">slice</em>, <em class="sig-param">ctx</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.Subscript" title="Permalink to this definition">¶</a></dt>
<dd><p>A subscript, such as <code class="docutils literal notranslate"><span class="pre">l[1]</span></code>. <code class="docutils literal notranslate"><span class="pre">value</span></code> is the subscripted object
(usually sequence or mapping). <code class="docutils literal notranslate"><span class="pre">slice</span></code> is an index, slice or key.
It can be a <a class="reference internal" href="#ast.Tuple" title="ast.Tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">Tuple</span></code></a> and contain a <a class="reference internal" href="#ast.Slice" title="ast.Slice"><code class="xref py py-class docutils literal notranslate"><span class="pre">Slice</span></code></a>.
<code class="docutils literal notranslate"><span class="pre">ctx</span></code> is <a class="reference internal" href="#ast.Load" title="ast.Load"><code class="xref py py-class docutils literal notranslate"><span class="pre">Load</span></code></a>, <a class="reference internal" href="#ast.Store" title="ast.Store"><code class="xref py py-class docutils literal notranslate"><span class="pre">Store</span></code></a> or <a class="reference internal" href="#ast.Del" title="ast.Del"><code class="xref py py-class docutils literal notranslate"><span class="pre">Del</span></code></a>
according to the action performed with the subscript.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;l[1:2, 3]&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;eval&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Expression(</span>
<span class="go">    body=Subscript(</span>
<span class="go">        value=Name(id=&#39;l&#39;, ctx=Load()),</span>
<span class="go">        slice=Tuple(</span>
<span class="go">            elts=[</span>
<span class="go">                Slice(</span>
<span class="go">                    lower=Constant(value=1),</span>
<span class="go">                    upper=Constant(value=2)),</span>
<span class="go">                Constant(value=3)],</span>
<span class="go">            ctx=Load()),</span>
<span class="go">        ctx=Load()))</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.Slice">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Slice</code><span class="sig-paren">(</span><em class="sig-param">lower</em>, <em class="sig-param">upper</em>, <em class="sig-param">step</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.Slice" title="Permalink to this definition">¶</a></dt>
<dd><p>Regular slicing (on the form <code class="docutils literal notranslate"><span class="pre">lower:upper</span></code> or <code class="docutils literal notranslate"><span class="pre">lower:upper:step</span></code>).
Can occur only inside the <em>slice</em> field of <a class="reference internal" href="#ast.Subscript" title="ast.Subscript"><code class="xref py py-class docutils literal notranslate"><span class="pre">Subscript</span></code></a>, either
directly or as an element of <a class="reference internal" href="#ast.Tuple" title="ast.Tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">Tuple</span></code></a>.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;l[1:2]&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;eval&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Expression(</span>
<span class="go">    body=Subscript(</span>
<span class="go">        value=Name(id=&#39;l&#39;, ctx=Load()),</span>
<span class="go">        slice=Slice(</span>
<span class="go">            lower=Constant(value=1),</span>
<span class="go">            upper=Constant(value=2)),</span>
<span class="go">        ctx=Load()))</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="comprehensions">
<h4>Comprehensions<a class="headerlink" href="#comprehensions" title="Permalink to this headline">¶</a></h4>
<dl class="class">
<dt id="ast.ListComp">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">ListComp</code><span class="sig-paren">(</span><em class="sig-param">elt</em>, <em class="sig-param">generators</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.ListComp" title="Permalink to this definition">¶</a></dt>
<dt id="ast.SetComp">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">SetComp</code><span class="sig-paren">(</span><em class="sig-param">elt</em>, <em class="sig-param">generators</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.SetComp" title="Permalink to this definition">¶</a></dt>
<dt id="ast.GeneratorExp">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">GeneratorExp</code><span class="sig-paren">(</span><em class="sig-param">elt</em>, <em class="sig-param">generators</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.GeneratorExp" title="Permalink to this definition">¶</a></dt>
<dt id="ast.DictComp">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">DictComp</code><span class="sig-paren">(</span><em class="sig-param">key</em>, <em class="sig-param">value</em>, <em class="sig-param">generators</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.DictComp" title="Permalink to this definition">¶</a></dt>
<dd><p>List and set comprehensions, generator expressions, and dictionary
comprehensions. <code class="docutils literal notranslate"><span class="pre">elt</span></code> (or <code class="docutils literal notranslate"><span class="pre">key</span></code> and <code class="docutils literal notranslate"><span class="pre">value</span></code>) is a single node
representing the part that will be evaluated for each item.</p>
<p><code class="docutils literal notranslate"><span class="pre">generators</span></code> is a list of <a class="reference internal" href="#ast.comprehension" title="ast.comprehension"><code class="xref py py-class docutils literal notranslate"><span class="pre">comprehension</span></code></a> nodes.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;[x for x in numbers]&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;eval&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Expression(</span>
<span class="go">    body=ListComp(</span>
<span class="go">        elt=Name(id=&#39;x&#39;, ctx=Load()),</span>
<span class="go">        generators=[</span>
<span class="go">            comprehension(</span>
<span class="go">                target=Name(id=&#39;x&#39;, ctx=Store()),</span>
<span class="go">                iter=Name(id=&#39;numbers&#39;, ctx=Load()),</span>
<span class="go">                ifs=[],</span>
<span class="go">                is_async=0)]))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;{x: x**2 for x in numbers}&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;eval&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Expression(</span>
<span class="go">    body=DictComp(</span>
<span class="go">        key=Name(id=&#39;x&#39;, ctx=Load()),</span>
<span class="go">        value=BinOp(</span>
<span class="go">            left=Name(id=&#39;x&#39;, ctx=Load()),</span>
<span class="go">            op=Pow(),</span>
<span class="go">            right=Constant(value=2)),</span>
<span class="go">        generators=[</span>
<span class="go">            comprehension(</span>
<span class="go">                target=Name(id=&#39;x&#39;, ctx=Store()),</span>
<span class="go">                iter=Name(id=&#39;numbers&#39;, ctx=Load()),</span>
<span class="go">                ifs=[],</span>
<span class="go">                is_async=0)]))</span>
<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;{x for x in numbers}&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;eval&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Expression(</span>
<span class="go">    body=SetComp(</span>
<span class="go">        elt=Name(id=&#39;x&#39;, ctx=Load()),</span>
<span class="go">        generators=[</span>
<span class="go">            comprehension(</span>
<span class="go">                target=Name(id=&#39;x&#39;, ctx=Store()),</span>
<span class="go">                iter=Name(id=&#39;numbers&#39;, ctx=Load()),</span>
<span class="go">                ifs=[],</span>
<span class="go">                is_async=0)]))</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.comprehension">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">comprehension</code><span class="sig-paren">(</span><em class="sig-param">target</em>, <em class="sig-param">iter</em>, <em class="sig-param">ifs</em>, <em class="sig-param">is_async</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.comprehension" title="Permalink to this definition">¶</a></dt>
<dd><p>One <code class="docutils literal notranslate"><span class="pre">for</span></code> clause in a comprehension. <code class="docutils literal notranslate"><span class="pre">target</span></code> is the reference to use for
each element - typically a <a class="reference internal" href="#ast.Name" title="ast.Name"><code class="xref py py-class docutils literal notranslate"><span class="pre">Name</span></code></a> or <a class="reference internal" href="#ast.Tuple" title="ast.Tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">Tuple</span></code></a> node. <code class="docutils literal notranslate"><span class="pre">iter</span></code>
is the object to iterate over. <code class="docutils literal notranslate"><span class="pre">ifs</span></code> is a list of test expressions: each
<code class="docutils literal notranslate"><span class="pre">for</span></code> clause can have multiple <code class="docutils literal notranslate"><span class="pre">ifs</span></code>.</p>
<p><code class="docutils literal notranslate"><span class="pre">is_async</span></code> indicates a comprehension is asynchronous (using an
<code class="docutils literal notranslate"><span class="pre">async</span> <span class="pre">for</span></code> instead of <code class="docutils literal notranslate"><span class="pre">for</span></code>). The value is an integer (0 or 1).</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;[ord(c) for line in file for c in line]&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;eval&#39;</span><span class="p">),</span>
<span class="gp">... </span>               <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span> <span class="c1"># Multiple comprehensions in one.</span>
<span class="go">Expression(</span>
<span class="go">    body=ListComp(</span>
<span class="go">        elt=Call(</span>
<span class="go">            func=Name(id=&#39;ord&#39;, ctx=Load()),</span>
<span class="go">            args=[</span>
<span class="go">                Name(id=&#39;c&#39;, ctx=Load())],</span>
<span class="go">            keywords=[]),</span>
<span class="go">        generators=[</span>
<span class="go">            comprehension(</span>
<span class="go">                target=Name(id=&#39;line&#39;, ctx=Store()),</span>
<span class="go">                iter=Name(id=&#39;file&#39;, ctx=Load()),</span>
<span class="go">                ifs=[],</span>
<span class="go">                is_async=0),</span>
<span class="go">            comprehension(</span>
<span class="go">                target=Name(id=&#39;c&#39;, ctx=Store()),</span>
<span class="go">                iter=Name(id=&#39;line&#39;, ctx=Load()),</span>
<span class="go">                ifs=[],</span>
<span class="go">                is_async=0)]))</span>

<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;(n**2 for n in it if n&gt;5 if n&lt;10)&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;eval&#39;</span><span class="p">),</span>
<span class="gp">... </span>               <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span> <span class="c1"># generator comprehension</span>
<span class="go">Expression(</span>
<span class="go">    body=GeneratorExp(</span>
<span class="go">        elt=BinOp(</span>
<span class="go">            left=Name(id=&#39;n&#39;, ctx=Load()),</span>
<span class="go">            op=Pow(),</span>
<span class="go">            right=Constant(value=2)),</span>
<span class="go">        generators=[</span>
<span class="go">            comprehension(</span>
<span class="go">                target=Name(id=&#39;n&#39;, ctx=Store()),</span>
<span class="go">                iter=Name(id=&#39;it&#39;, ctx=Load()),</span>
<span class="go">                ifs=[</span>
<span class="go">                    Compare(</span>
<span class="go">                        left=Name(id=&#39;n&#39;, ctx=Load()),</span>
<span class="go">                        ops=[</span>
<span class="go">                            Gt()],</span>
<span class="go">                        comparators=[</span>
<span class="go">                            Constant(value=5)]),</span>
<span class="go">                    Compare(</span>
<span class="go">                        left=Name(id=&#39;n&#39;, ctx=Load()),</span>
<span class="go">                        ops=[</span>
<span class="go">                            Lt()],</span>
<span class="go">                        comparators=[</span>
<span class="go">                            Constant(value=10)])],</span>
<span class="go">                is_async=0)]))</span>

<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;[i async for i in soc]&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;eval&#39;</span><span class="p">),</span>
<span class="gp">... </span>               <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span> <span class="c1"># Async comprehension</span>
<span class="go">Expression(</span>
<span class="go">    body=ListComp(</span>
<span class="go">        elt=Name(id=&#39;i&#39;, ctx=Load()),</span>
<span class="go">        generators=[</span>
<span class="go">            comprehension(</span>
<span class="go">                target=Name(id=&#39;i&#39;, ctx=Store()),</span>
<span class="go">                iter=Name(id=&#39;soc&#39;, ctx=Load()),</span>
<span class="go">                ifs=[],</span>
<span class="go">                is_async=1)]))</span>
</pre></div>
</div>
</dd></dl>

</section>
</section>
<section id="statements">
<h3>Statements<a class="headerlink" href="#statements" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="ast.Assign">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Assign</code><span class="sig-paren">(</span><em class="sig-param">targets</em>, <em class="sig-param">value</em>, <em class="sig-param">type_comment</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.Assign" title="Permalink to this definition">¶</a></dt>
<dd><p>An assignment. <code class="docutils literal notranslate"><span class="pre">targets</span></code> is a list of nodes, and <code class="docutils literal notranslate"><span class="pre">value</span></code> is a single node.</p>
<p>Multiple nodes in <code class="docutils literal notranslate"><span class="pre">targets</span></code> represents assigning the same value to each.
Unpacking is represented by putting a <a class="reference internal" href="#ast.Tuple" title="ast.Tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">Tuple</span></code></a> or <a class="reference internal" href="#ast.List" title="ast.List"><code class="xref py py-class docutils literal notranslate"><span class="pre">List</span></code></a>
within <code class="docutils literal notranslate"><span class="pre">targets</span></code>.</p>
<dl class="attribute">
<dt id="ast.Assign.type_comment">
<code class="sig-name descname">type_comment</code><a class="headerlink" href="#ast.Assign.type_comment" title="Permalink to this definition">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">type_comment</span></code> is an optional string with the type annotation as a comment.</p>
</dd></dl>

<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;a = b = 1&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span> <span class="c1"># Multiple assignment</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        Assign(</span>
<span class="go">            targets=[</span>
<span class="go">                Name(id=&#39;a&#39;, ctx=Store()),</span>
<span class="go">                Name(id=&#39;b&#39;, ctx=Store())],</span>
<span class="go">            value=Constant(value=1))],</span>
<span class="go">    type_ignores=[])</span>

<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;a,b = c&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span> <span class="c1"># Unpacking</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        Assign(</span>
<span class="go">            targets=[</span>
<span class="go">                Tuple(</span>
<span class="go">                    elts=[</span>
<span class="go">                        Name(id=&#39;a&#39;, ctx=Store()),</span>
<span class="go">                        Name(id=&#39;b&#39;, ctx=Store())],</span>
<span class="go">                    ctx=Store())],</span>
<span class="go">            value=Name(id=&#39;c&#39;, ctx=Load()))],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.AnnAssign">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">AnnAssign</code><span class="sig-paren">(</span><em class="sig-param">target</em>, <em class="sig-param">annotation</em>, <em class="sig-param">value</em>, <em class="sig-param">simple</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.AnnAssign" title="Permalink to this definition">¶</a></dt>
<dd><p>An assignment with a type annotation. <code class="docutils literal notranslate"><span class="pre">target</span></code> is a single node and can
be a <a class="reference internal" href="#ast.Name" title="ast.Name"><code class="xref py py-class docutils literal notranslate"><span class="pre">Name</span></code></a>, a <a class="reference internal" href="#ast.Attribute" title="ast.Attribute"><code class="xref py py-class docutils literal notranslate"><span class="pre">Attribute</span></code></a> or a <a class="reference internal" href="#ast.Subscript" title="ast.Subscript"><code class="xref py py-class docutils literal notranslate"><span class="pre">Subscript</span></code></a>.
<code class="docutils literal notranslate"><span class="pre">annotation</span></code> is the annotation, such as a <a class="reference internal" href="#ast.Constant" title="ast.Constant"><code class="xref py py-class docutils literal notranslate"><span class="pre">Constant</span></code></a> or <a class="reference internal" href="#ast.Name" title="ast.Name"><code class="xref py py-class docutils literal notranslate"><span class="pre">Name</span></code></a>
node. <code class="docutils literal notranslate"><span class="pre">value</span></code> is a single optional node. <code class="docutils literal notranslate"><span class="pre">simple</span></code> is a boolean integer
set to True for a <a class="reference internal" href="#ast.Name" title="ast.Name"><code class="xref py py-class docutils literal notranslate"><span class="pre">Name</span></code></a> node in <code class="docutils literal notranslate"><span class="pre">target</span></code> that do not appear in
between parenthesis and are hence pure names and not expressions.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;c: int&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        AnnAssign(</span>
<span class="go">            target=Name(id=&#39;c&#39;, ctx=Store()),</span>
<span class="go">            annotation=Name(id=&#39;int&#39;, ctx=Load()),</span>
<span class="go">            simple=1)],</span>
<span class="go">    type_ignores=[])</span>

<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;(a): int = 1&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span> <span class="c1"># Annotation with parenthesis</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        AnnAssign(</span>
<span class="go">            target=Name(id=&#39;a&#39;, ctx=Store()),</span>
<span class="go">            annotation=Name(id=&#39;int&#39;, ctx=Load()),</span>
<span class="go">            value=Constant(value=1),</span>
<span class="go">            simple=0)],</span>
<span class="go">    type_ignores=[])</span>

<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;a.b: int&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span> <span class="c1"># Attribute annotation</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        AnnAssign(</span>
<span class="go">            target=Attribute(</span>
<span class="go">                value=Name(id=&#39;a&#39;, ctx=Load()),</span>
<span class="go">                attr=&#39;b&#39;,</span>
<span class="go">                ctx=Store()),</span>
<span class="go">            annotation=Name(id=&#39;int&#39;, ctx=Load()),</span>
<span class="go">            simple=0)],</span>
<span class="go">    type_ignores=[])</span>

<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;a[1]: int&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span> <span class="c1"># Subscript annotation</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        AnnAssign(</span>
<span class="go">            target=Subscript(</span>
<span class="go">                value=Name(id=&#39;a&#39;, ctx=Load()),</span>
<span class="go">                slice=Constant(value=1),</span>
<span class="go">                ctx=Store()),</span>
<span class="go">            annotation=Name(id=&#39;int&#39;, ctx=Load()),</span>
<span class="go">            simple=0)],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.AugAssign">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">AugAssign</code><span class="sig-paren">(</span><em class="sig-param">target</em>, <em class="sig-param">op</em>, <em class="sig-param">value</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.AugAssign" title="Permalink to this definition">¶</a></dt>
<dd><p>Augmented assignment, such as <code class="docutils literal notranslate"><span class="pre">a</span> <span class="pre">+=</span> <span class="pre">1</span></code>. In the following example,
<code class="docutils literal notranslate"><span class="pre">target</span></code> is a <a class="reference internal" href="#ast.Name" title="ast.Name"><code class="xref py py-class docutils literal notranslate"><span class="pre">Name</span></code></a> node for <code class="docutils literal notranslate"><span class="pre">x</span></code> (with the <a class="reference internal" href="#ast.Store" title="ast.Store"><code class="xref py py-class docutils literal notranslate"><span class="pre">Store</span></code></a>
context), <code class="docutils literal notranslate"><span class="pre">op</span></code> is <a class="reference internal" href="#ast.Add" title="ast.Add"><code class="xref py py-class docutils literal notranslate"><span class="pre">Add</span></code></a>, and <code class="docutils literal notranslate"><span class="pre">value</span></code> is a <a class="reference internal" href="#ast.Constant" title="ast.Constant"><code class="xref py py-class docutils literal notranslate"><span class="pre">Constant</span></code></a> with
value for 1.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">target</span></code> attribute connot be of class <a class="reference internal" href="#ast.Tuple" title="ast.Tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">Tuple</span></code></a> or <a class="reference internal" href="#ast.List" title="ast.List"><code class="xref py py-class docutils literal notranslate"><span class="pre">List</span></code></a>,
unlike the targets of <a class="reference internal" href="#ast.Assign" title="ast.Assign"><code class="xref py py-class docutils literal notranslate"><span class="pre">Assign</span></code></a>.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;x += 2&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        AugAssign(</span>
<span class="go">            target=Name(id=&#39;x&#39;, ctx=Store()),</span>
<span class="go">            op=Add(),</span>
<span class="go">            value=Constant(value=2))],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.Raise">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Raise</code><span class="sig-paren">(</span><em class="sig-param">exc</em>, <em class="sig-param">cause</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.Raise" title="Permalink to this definition">¶</a></dt>
<dd><p>A <code class="docutils literal notranslate"><span class="pre">raise</span></code> statement. <code class="docutils literal notranslate"><span class="pre">exc</span></code> is the exception object to be raised, normally a
<a class="reference internal" href="#ast.Call" title="ast.Call"><code class="xref py py-class docutils literal notranslate"><span class="pre">Call</span></code></a> or <a class="reference internal" href="#ast.Name" title="ast.Name"><code class="xref py py-class docutils literal notranslate"><span class="pre">Name</span></code></a>, or <code class="docutils literal notranslate"><span class="pre">None</span></code> for a standalone <code class="docutils literal notranslate"><span class="pre">raise</span></code>.
<code class="docutils literal notranslate"><span class="pre">cause</span></code> is the optional part for <code class="docutils literal notranslate"><span class="pre">y</span></code> in <code class="docutils literal notranslate"><span class="pre">raise</span> <span class="pre">x</span> <span class="pre">from</span> <span class="pre">y</span></code>.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;raise x from y&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        Raise(</span>
<span class="go">            exc=Name(id=&#39;x&#39;, ctx=Load()),</span>
<span class="go">            cause=Name(id=&#39;y&#39;, ctx=Load()))],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.Assert">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Assert</code><span class="sig-paren">(</span><em class="sig-param">test</em>, <em class="sig-param">msg</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.Assert" title="Permalink to this definition">¶</a></dt>
<dd><p>An assertion. <code class="docutils literal notranslate"><span class="pre">test</span></code> holds the condition, such as a <a class="reference internal" href="#ast.Compare" title="ast.Compare"><code class="xref py py-class docutils literal notranslate"><span class="pre">Compare</span></code></a> node.
<code class="docutils literal notranslate"><span class="pre">msg</span></code> holds the failure message.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;assert x,y&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        Assert(</span>
<span class="go">            test=Name(id=&#39;x&#39;, ctx=Load()),</span>
<span class="go">            msg=Name(id=&#39;y&#39;, ctx=Load()))],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.Delete">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Delete</code><span class="sig-paren">(</span><em class="sig-param">targets</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.Delete" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents a <code class="docutils literal notranslate"><span class="pre">del</span></code> statement. <code class="docutils literal notranslate"><span class="pre">targets</span></code> is a list of nodes, such as
<a class="reference internal" href="#ast.Name" title="ast.Name"><code class="xref py py-class docutils literal notranslate"><span class="pre">Name</span></code></a>, <a class="reference internal" href="#ast.Attribute" title="ast.Attribute"><code class="xref py py-class docutils literal notranslate"><span class="pre">Attribute</span></code></a> or <a class="reference internal" href="#ast.Subscript" title="ast.Subscript"><code class="xref py py-class docutils literal notranslate"><span class="pre">Subscript</span></code></a> nodes.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;del x,y,z&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        Delete(</span>
<span class="go">            targets=[</span>
<span class="go">                Name(id=&#39;x&#39;, ctx=Del()),</span>
<span class="go">                Name(id=&#39;y&#39;, ctx=Del()),</span>
<span class="go">                Name(id=&#39;z&#39;, ctx=Del())])],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.Pass">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Pass</code><a class="headerlink" href="#ast.Pass" title="Permalink to this definition">¶</a></dt>
<dd><p>A <code class="docutils literal notranslate"><span class="pre">pass</span></code> statement.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;pass&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        Pass()],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

<p>Other statements which are only applicable inside functions or loops are
described in other sections.</p>
<section id="imports">
<h4>Imports<a class="headerlink" href="#imports" title="Permalink to this headline">¶</a></h4>
<dl class="class">
<dt id="ast.Import">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Import</code><span class="sig-paren">(</span><em class="sig-param">names</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.Import" title="Permalink to this definition">¶</a></dt>
<dd><p>An import statement. <code class="docutils literal notranslate"><span class="pre">names</span></code> is a list of <a class="reference internal" href="#ast.alias" title="ast.alias"><code class="xref py py-class docutils literal notranslate"><span class="pre">alias</span></code></a> nodes.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;import x,y,z&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        Import(</span>
<span class="go">            names=[</span>
<span class="go">                alias(name=&#39;x&#39;),</span>
<span class="go">                alias(name=&#39;y&#39;),</span>
<span class="go">                alias(name=&#39;z&#39;)])],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.ImportFrom">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">ImportFrom</code><span class="sig-paren">(</span><em class="sig-param">module</em>, <em class="sig-param">names</em>, <em class="sig-param">level</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.ImportFrom" title="Permalink to this definition">¶</a></dt>
<dd><p>Represents <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">x</span> <span class="pre">import</span> <span class="pre">y</span></code>. <code class="docutils literal notranslate"><span class="pre">module</span></code> is a raw string of the ‘from’ name,
without any leading dots, or <code class="docutils literal notranslate"><span class="pre">None</span></code> for statements such as <code class="docutils literal notranslate"><span class="pre">from</span> <span class="pre">.</span> <span class="pre">import</span> <span class="pre">foo</span></code>.
<code class="docutils literal notranslate"><span class="pre">level</span></code> is an integer holding the level of the relative import (0 means
absolute import).</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;from y import x,y,z&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        ImportFrom(</span>
<span class="go">            module=&#39;y&#39;,</span>
<span class="go">            names=[</span>
<span class="go">                alias(name=&#39;x&#39;),</span>
<span class="go">                alias(name=&#39;y&#39;),</span>
<span class="go">                alias(name=&#39;z&#39;)],</span>
<span class="go">            level=0)],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.alias">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">alias</code><span class="sig-paren">(</span><em class="sig-param">name</em>, <em class="sig-param">asname</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.alias" title="Permalink to this definition">¶</a></dt>
<dd><p>Both parameters are raw strings of the names. <code class="docutils literal notranslate"><span class="pre">asname</span></code> can be <code class="docutils literal notranslate"><span class="pre">None</span></code> if
the regular name is to be used.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;from ..foo.bar import a as b, c&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        ImportFrom(</span>
<span class="go">            module=&#39;foo.bar&#39;,</span>
<span class="go">            names=[</span>
<span class="go">                alias(name=&#39;a&#39;, asname=&#39;b&#39;),</span>
<span class="go">                alias(name=&#39;c&#39;)],</span>
<span class="go">            level=2)],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

</section>
</section>
<section id="control-flow">
<h3>Control flow<a class="headerlink" href="#control-flow" title="Permalink to this headline">¶</a></h3>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>Optional clauses such as <code class="docutils literal notranslate"><span class="pre">else</span></code> are stored as an empty list if they’re
not present.</p>
</div>
<dl class="class">
<dt id="ast.If">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">If</code><span class="sig-paren">(</span><em class="sig-param">test</em>, <em class="sig-param">body</em>, <em class="sig-param">orelse</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.If" title="Permalink to this definition">¶</a></dt>
<dd><p>An <code class="docutils literal notranslate"><span class="pre">if</span></code> statement. <code class="docutils literal notranslate"><span class="pre">test</span></code> holds a single node, such as a <a class="reference internal" href="#ast.Compare" title="ast.Compare"><code class="xref py py-class docutils literal notranslate"><span class="pre">Compare</span></code></a>
node. <code class="docutils literal notranslate"><span class="pre">body</span></code> and <code class="docutils literal notranslate"><span class="pre">orelse</span></code> each hold a list of nodes.</p>
<p><code class="docutils literal notranslate"><span class="pre">elif</span></code> clauses don’t have a special representation in the AST, but rather
appear as extra <a class="reference internal" href="#ast.If" title="ast.If"><code class="xref py py-class docutils literal notranslate"><span class="pre">If</span></code></a> nodes within the <code class="docutils literal notranslate"><span class="pre">orelse</span></code> section of the
previous one.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span>
<span class="gp">... </span><span class="s2">if x:</span>
<span class="gp">... </span><span class="s2">   ...</span>
<span class="gp">... </span><span class="s2">elif y:</span>
<span class="gp">... </span><span class="s2">   ...</span>
<span class="gp">... </span><span class="s2">else:</span>
<span class="gp">... </span><span class="s2">   ...</span>
<span class="gp">... </span><span class="s2">&quot;&quot;&quot;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        If(</span>
<span class="go">            test=Name(id=&#39;x&#39;, ctx=Load()),</span>
<span class="go">            body=[</span>
<span class="go">                Expr(</span>
<span class="go">                    value=Constant(value=Ellipsis))],</span>
<span class="go">            orelse=[</span>
<span class="go">                If(</span>
<span class="go">                    test=Name(id=&#39;y&#39;, ctx=Load()),</span>
<span class="go">                    body=[</span>
<span class="go">                        Expr(</span>
<span class="go">                            value=Constant(value=Ellipsis))],</span>
<span class="go">                    orelse=[</span>
<span class="go">                        Expr(</span>
<span class="go">                            value=Constant(value=Ellipsis))])])],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.For">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">For</code><span class="sig-paren">(</span><em class="sig-param">target</em>, <em class="sig-param">iter</em>, <em class="sig-param">body</em>, <em class="sig-param">orelse</em>, <em class="sig-param">type_comment</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.For" title="Permalink to this definition">¶</a></dt>
<dd><p>A <code class="docutils literal notranslate"><span class="pre">for</span></code> loop. <code class="docutils literal notranslate"><span class="pre">target</span></code> holds the variable(s) the loop assigns to, as a
single <a class="reference internal" href="#ast.Name" title="ast.Name"><code class="xref py py-class docutils literal notranslate"><span class="pre">Name</span></code></a>, <a class="reference internal" href="#ast.Tuple" title="ast.Tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">Tuple</span></code></a> or <a class="reference internal" href="#ast.List" title="ast.List"><code class="xref py py-class docutils literal notranslate"><span class="pre">List</span></code></a> node. <code class="docutils literal notranslate"><span class="pre">iter</span></code> holds
the item to be looped over, again as a single node. <code class="docutils literal notranslate"><span class="pre">body</span></code> and <code class="docutils literal notranslate"><span class="pre">orelse</span></code>
contain lists of nodes to execute. Those in <code class="docutils literal notranslate"><span class="pre">orelse</span></code> are executed if the
loop finishes normally, rather than via a <code class="docutils literal notranslate"><span class="pre">break</span></code> statement.</p>
<dl class="attribute">
<dt id="ast.For.type_comment">
<code class="sig-name descname">type_comment</code><a class="headerlink" href="#ast.For.type_comment" title="Permalink to this definition">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">type_comment</span></code> is an optional string with the type annotation as a comment.</p>
</dd></dl>

<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span>
<span class="gp">... </span><span class="s2">for x in y:</span>
<span class="gp">... </span><span class="s2">    ...</span>
<span class="gp">... </span><span class="s2">else:</span>
<span class="gp">... </span><span class="s2">    ...</span>
<span class="gp">... </span><span class="s2">&quot;&quot;&quot;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        For(</span>
<span class="go">            target=Name(id=&#39;x&#39;, ctx=Store()),</span>
<span class="go">            iter=Name(id=&#39;y&#39;, ctx=Load()),</span>
<span class="go">            body=[</span>
<span class="go">                Expr(</span>
<span class="go">                    value=Constant(value=Ellipsis))],</span>
<span class="go">            orelse=[</span>
<span class="go">                Expr(</span>
<span class="go">                    value=Constant(value=Ellipsis))])],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.While">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">While</code><span class="sig-paren">(</span><em class="sig-param">test</em>, <em class="sig-param">body</em>, <em class="sig-param">orelse</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.While" title="Permalink to this definition">¶</a></dt>
<dd><p>A <code class="docutils literal notranslate"><span class="pre">while</span></code> loop. <code class="docutils literal notranslate"><span class="pre">test</span></code> holds the condition, such as a <a class="reference internal" href="#ast.Compare" title="ast.Compare"><code class="xref py py-class docutils literal notranslate"><span class="pre">Compare</span></code></a>
node.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="go">&gt;&gt; print(ast.dump(ast.parse(&quot;&quot;&quot;</span>
<span class="gp">... </span><span class="k">while</span> <span class="n">x</span><span class="p">:</span>
<span class="gp">... </span>   <span class="o">...</span>
<span class="gp">... </span><span class="k">else</span><span class="p">:</span>
<span class="gp">... </span>   <span class="o">...</span>
<span class="gp">... </span><span class="s2">&quot;&quot;&quot;), indent=4))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        While(</span>
<span class="go">            test=Name(id=&#39;x&#39;, ctx=Load()),</span>
<span class="go">            body=[</span>
<span class="go">                Expr(</span>
<span class="go">                    value=Constant(value=Ellipsis))],</span>
<span class="go">            orelse=[</span>
<span class="go">                Expr(</span>
<span class="go">                    value=Constant(value=Ellipsis))])],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.Break">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Break</code><a class="headerlink" href="#ast.Break" title="Permalink to this definition">¶</a></dt>
<dt id="ast.Continue">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Continue</code><a class="headerlink" href="#ast.Continue" title="Permalink to this definition">¶</a></dt>
<dd><p>The <code class="docutils literal notranslate"><span class="pre">break</span></code> and <code class="docutils literal notranslate"><span class="pre">continue</span></code> statements.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span><span class="se">\</span>
<span class="gp">... </span><span class="s2">for a in b:</span>
<span class="gp">... </span><span class="s2">    if a &gt; 5:</span>
<span class="gp">... </span><span class="s2">        break</span>
<span class="gp">... </span><span class="s2">    else:</span>
<span class="gp">... </span><span class="s2">        continue</span>
<span class="gp">...</span><span class="s2"></span>
<span class="gp">... </span><span class="s2">&quot;&quot;&quot;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        For(</span>
<span class="go">            target=Name(id=&#39;a&#39;, ctx=Store()),</span>
<span class="go">            iter=Name(id=&#39;b&#39;, ctx=Load()),</span>
<span class="go">            body=[</span>
<span class="go">                If(</span>
<span class="go">                    test=Compare(</span>
<span class="go">                        left=Name(id=&#39;a&#39;, ctx=Load()),</span>
<span class="go">                        ops=[</span>
<span class="go">                            Gt()],</span>
<span class="go">                        comparators=[</span>
<span class="go">                            Constant(value=5)]),</span>
<span class="go">                    body=[</span>
<span class="go">                        Break()],</span>
<span class="go">                    orelse=[</span>
<span class="go">                        Continue()])],</span>
<span class="go">            orelse=[])],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.Try">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Try</code><span class="sig-paren">(</span><em class="sig-param">body</em>, <em class="sig-param">handlers</em>, <em class="sig-param">orelse</em>, <em class="sig-param">finalbody</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.Try" title="Permalink to this definition">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">try</span></code> blocks. All attributes are list of nodes to execute, except for
<code class="docutils literal notranslate"><span class="pre">handlers</span></code>, which is a list of <a class="reference internal" href="#ast.ExceptHandler" title="ast.ExceptHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">ExceptHandler</span></code></a> nodes.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span>
<span class="gp">... </span><span class="s2">try:</span>
<span class="gp">... </span><span class="s2">   ...</span>
<span class="gp">... </span><span class="s2">except Exception:</span>
<span class="gp">... </span><span class="s2">   ...</span>
<span class="gp">... </span><span class="s2">except OtherException as e:</span>
<span class="gp">... </span><span class="s2">   ...</span>
<span class="gp">... </span><span class="s2">else:</span>
<span class="gp">... </span><span class="s2">   ...</span>
<span class="gp">... </span><span class="s2">finally:</span>
<span class="gp">... </span><span class="s2">   ...</span>
<span class="gp">... </span><span class="s2">&quot;&quot;&quot;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        Try(</span>
<span class="go">            body=[</span>
<span class="go">                Expr(</span>
<span class="go">                    value=Constant(value=Ellipsis))],</span>
<span class="go">            handlers=[</span>
<span class="go">                ExceptHandler(</span>
<span class="go">                    type=Name(id=&#39;Exception&#39;, ctx=Load()),</span>
<span class="go">                    body=[</span>
<span class="go">                        Expr(</span>
<span class="go">                            value=Constant(value=Ellipsis))]),</span>
<span class="go">                ExceptHandler(</span>
<span class="go">                    type=Name(id=&#39;OtherException&#39;, ctx=Load()),</span>
<span class="go">                    name=&#39;e&#39;,</span>
<span class="go">                    body=[</span>
<span class="go">                        Expr(</span>
<span class="go">                            value=Constant(value=Ellipsis))])],</span>
<span class="go">            orelse=[</span>
<span class="go">                Expr(</span>
<span class="go">                    value=Constant(value=Ellipsis))],</span>
<span class="go">            finalbody=[</span>
<span class="go">                Expr(</span>
<span class="go">                    value=Constant(value=Ellipsis))])],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.ExceptHandler">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">ExceptHandler</code><span class="sig-paren">(</span><em class="sig-param">type</em>, <em class="sig-param">name</em>, <em class="sig-param">body</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.ExceptHandler" title="Permalink to this definition">¶</a></dt>
<dd><p>A single <code class="docutils literal notranslate"><span class="pre">except</span></code> clause. <code class="docutils literal notranslate"><span class="pre">type</span></code> is the exception type it will match,
typically a <a class="reference internal" href="#ast.Name" title="ast.Name"><code class="xref py py-class docutils literal notranslate"><span class="pre">Name</span></code></a> node (or <code class="docutils literal notranslate"><span class="pre">None</span></code> for a catch-all <code class="docutils literal notranslate"><span class="pre">except:</span></code> clause).
<code class="docutils literal notranslate"><span class="pre">name</span></code> is a raw string for the name to hold the exception, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if
the clause doesn’t have <code class="docutils literal notranslate"><span class="pre">as</span> <span class="pre">foo</span></code>. <code class="docutils literal notranslate"><span class="pre">body</span></code> is a list of nodes.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span><span class="se">\</span>
<span class="gp">... </span><span class="s2">try:</span>
<span class="gp">... </span><span class="s2">    a + 1</span>
<span class="gp">... </span><span class="s2">except TypeError:</span>
<span class="gp">... </span><span class="s2">    pass</span>
<span class="gp">... </span><span class="s2">&quot;&quot;&quot;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        Try(</span>
<span class="go">            body=[</span>
<span class="go">                Expr(</span>
<span class="go">                    value=BinOp(</span>
<span class="go">                        left=Name(id=&#39;a&#39;, ctx=Load()),</span>
<span class="go">                        op=Add(),</span>
<span class="go">                        right=Constant(value=1)))],</span>
<span class="go">            handlers=[</span>
<span class="go">                ExceptHandler(</span>
<span class="go">                    type=Name(id=&#39;TypeError&#39;, ctx=Load()),</span>
<span class="go">                    body=[</span>
<span class="go">                        Pass()])],</span>
<span class="go">            orelse=[],</span>
<span class="go">            finalbody=[])],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.With">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">With</code><span class="sig-paren">(</span><em class="sig-param">items</em>, <em class="sig-param">body</em>, <em class="sig-param">type_comment</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.With" title="Permalink to this definition">¶</a></dt>
<dd><p>A <code class="docutils literal notranslate"><span class="pre">with</span></code> block. <code class="docutils literal notranslate"><span class="pre">items</span></code> is a list of <a class="reference internal" href="#ast.withitem" title="ast.withitem"><code class="xref py py-class docutils literal notranslate"><span class="pre">withitem</span></code></a> nodes representing
the context managers, and <code class="docutils literal notranslate"><span class="pre">body</span></code> is the indented block inside the context.</p>
<dl class="attribute">
<dt id="ast.With.type_comment">
<code class="sig-name descname">type_comment</code><a class="headerlink" href="#ast.With.type_comment" title="Permalink to this definition">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">type_comment</span></code> is an optional string with the type annotation as a comment.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="ast.withitem">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">withitem</code><span class="sig-paren">(</span><em class="sig-param">context_expr</em>, <em class="sig-param">optional_vars</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.withitem" title="Permalink to this definition">¶</a></dt>
<dd><p>A single context manager in a <code class="docutils literal notranslate"><span class="pre">with</span></code> block. <code class="docutils literal notranslate"><span class="pre">context_expr</span></code> is the context
manager, often a <a class="reference internal" href="#ast.Call" title="ast.Call"><code class="xref py py-class docutils literal notranslate"><span class="pre">Call</span></code></a> node. <code class="docutils literal notranslate"><span class="pre">optional_vars</span></code> is a <a class="reference internal" href="#ast.Name" title="ast.Name"><code class="xref py py-class docutils literal notranslate"><span class="pre">Name</span></code></a>,
<a class="reference internal" href="#ast.Tuple" title="ast.Tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">Tuple</span></code></a> or <a class="reference internal" href="#ast.List" title="ast.List"><code class="xref py py-class docutils literal notranslate"><span class="pre">List</span></code></a> for the <code class="docutils literal notranslate"><span class="pre">as</span> <span class="pre">foo</span></code> part, or <code class="docutils literal notranslate"><span class="pre">None</span></code> if that
isn’t used.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span><span class="se">\</span>
<span class="gp">... </span><span class="s2">with a as b, c as d:</span>
<span class="gp">... </span><span class="s2">   something(b, d)</span>
<span class="gp">... </span><span class="s2">&quot;&quot;&quot;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        With(</span>
<span class="go">            items=[</span>
<span class="go">                withitem(</span>
<span class="go">                    context_expr=Name(id=&#39;a&#39;, ctx=Load()),</span>
<span class="go">                    optional_vars=Name(id=&#39;b&#39;, ctx=Store())),</span>
<span class="go">                withitem(</span>
<span class="go">                    context_expr=Name(id=&#39;c&#39;, ctx=Load()),</span>
<span class="go">                    optional_vars=Name(id=&#39;d&#39;, ctx=Store()))],</span>
<span class="go">            body=[</span>
<span class="go">                Expr(</span>
<span class="go">                    value=Call(</span>
<span class="go">                        func=Name(id=&#39;something&#39;, ctx=Load()),</span>
<span class="go">                        args=[</span>
<span class="go">                            Name(id=&#39;b&#39;, ctx=Load()),</span>
<span class="go">                            Name(id=&#39;d&#39;, ctx=Load())],</span>
<span class="go">                        keywords=[]))])],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="function-and-class-definitions">
<h3>Function and class definitions<a class="headerlink" href="#function-and-class-definitions" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="ast.FunctionDef">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">FunctionDef</code><span class="sig-paren">(</span><em class="sig-param">name</em>, <em class="sig-param">args</em>, <em class="sig-param">body</em>, <em class="sig-param">decorator_list</em>, <em class="sig-param">returns</em>, <em class="sig-param">type_comment</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.FunctionDef" title="Permalink to this definition">¶</a></dt>
<dd><p>A function definition.</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">name</span></code> is a raw string of the function name.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">args</span></code> is a <a class="reference internal" href="#ast.arguments" title="ast.arguments"><code class="xref py py-class docutils literal notranslate"><span class="pre">arguments</span></code></a> node.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">body</span></code> is the list of nodes inside the function.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">decorator_list</span></code> is the list of decorators to be applied, stored outermost
first (i.e. the first in the list will be applied last).</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">returns</span></code> is the return annotation.</p></li>
</ul>
<dl class="attribute">
<dt id="ast.FunctionDef.type_comment">
<code class="sig-name descname">type_comment</code><a class="headerlink" href="#ast.FunctionDef.type_comment" title="Permalink to this definition">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">type_comment</span></code> is an optional string with the type annotation as a comment.</p>
</dd></dl>

</dd></dl>

<dl class="class">
<dt id="ast.Lambda">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Lambda</code><span class="sig-paren">(</span><em class="sig-param">args</em>, <em class="sig-param">body</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.Lambda" title="Permalink to this definition">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">lambda</span></code> is a minimal function definition that can be used inside an
expression. Unlike <a class="reference internal" href="#ast.FunctionDef" title="ast.FunctionDef"><code class="xref py py-class docutils literal notranslate"><span class="pre">FunctionDef</span></code></a>, <code class="docutils literal notranslate"><span class="pre">body</span></code> holds a single node.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;lambda x,y: ...&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        Expr(</span>
<span class="go">            value=Lambda(</span>
<span class="go">                args=arguments(</span>
<span class="go">                    posonlyargs=[],</span>
<span class="go">                    args=[</span>
<span class="go">                        arg(arg=&#39;x&#39;),</span>
<span class="go">                        arg(arg=&#39;y&#39;)],</span>
<span class="go">                    kwonlyargs=[],</span>
<span class="go">                    kw_defaults=[],</span>
<span class="go">                    defaults=[]),</span>
<span class="go">                body=Constant(value=Ellipsis)))],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.arguments">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">arguments</code><span class="sig-paren">(</span><em class="sig-param">posonlyargs</em>, <em class="sig-param">args</em>, <em class="sig-param">vararg</em>, <em class="sig-param">kwonlyargs</em>, <em class="sig-param">kw_defaults</em>, <em class="sig-param">kwarg</em>, <em class="sig-param">defaults</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.arguments" title="Permalink to this definition">¶</a></dt>
<dd><p>The arguments for a function.</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">posonlyargs</span></code>, <code class="docutils literal notranslate"><span class="pre">args</span></code> and <code class="docutils literal notranslate"><span class="pre">kwonlyargs</span></code> are lists of <a class="reference internal" href="#ast.arg" title="ast.arg"><code class="xref py py-class docutils literal notranslate"><span class="pre">arg</span></code></a> nodes.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">vararg</span></code> and <code class="docutils literal notranslate"><span class="pre">kwarg</span></code> are single <a class="reference internal" href="#ast.arg" title="ast.arg"><code class="xref py py-class docutils literal notranslate"><span class="pre">arg</span></code></a> nodes, referring to the
<code class="docutils literal notranslate"><span class="pre">*args,</span> <span class="pre">**kwargs</span></code> parameters.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">kw_defaults</span></code> is a list of default values for keyword-only arguments. If
one is <code class="docutils literal notranslate"><span class="pre">None</span></code>, the corresponding argument is required.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">defaults</span></code> is a list of default values for arguments that can be passed
positionally. If there are fewer defaults, they correspond to the last n
arguments.</p></li>
</ul>
</dd></dl>

<dl class="class">
<dt id="ast.arg">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">arg</code><span class="sig-paren">(</span><em class="sig-param">arg</em>, <em class="sig-param">annotation</em>, <em class="sig-param">type_comment</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.arg" title="Permalink to this definition">¶</a></dt>
<dd><p>A single argument in a list. <code class="docutils literal notranslate"><span class="pre">arg</span></code> is a raw string of the argument
name, <code class="docutils literal notranslate"><span class="pre">annotation</span></code> is its annotation, such as a <code class="xref py py-class docutils literal notranslate"><span class="pre">Str</span></code> or
<a class="reference internal" href="#ast.Name" title="ast.Name"><code class="xref py py-class docutils literal notranslate"><span class="pre">Name</span></code></a> node.</p>
<dl class="attribute">
<dt id="ast.arg.type_comment">
<code class="sig-name descname">type_comment</code><a class="headerlink" href="#ast.arg.type_comment" title="Permalink to this definition">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">type_comment</span></code> is an optional string with the type annotation as a comment</p>
</dd></dl>

<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span><span class="se">\</span>
<span class="gp">... </span><span class="s2">@decorator1</span>
<span class="gp">... </span><span class="s2">@decorator2</span>
<span class="gp">... </span><span class="s2">def f(a: &#39;annotation&#39;, b=1, c=2, *d, e, f=3, **g) -&gt; &#39;return annotation&#39;:</span>
<span class="gp">... </span><span class="s2">    pass</span>
<span class="gp">... </span><span class="s2">&quot;&quot;&quot;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        FunctionDef(</span>
<span class="go">            name=&#39;f&#39;,</span>
<span class="go">            args=arguments(</span>
<span class="go">                posonlyargs=[],</span>
<span class="go">                args=[</span>
<span class="go">                    arg(</span>
<span class="go">                        arg=&#39;a&#39;,</span>
<span class="go">                        annotation=Constant(value=&#39;annotation&#39;)),</span>
<span class="go">                    arg(arg=&#39;b&#39;),</span>
<span class="go">                    arg(arg=&#39;c&#39;)],</span>
<span class="go">                vararg=arg(arg=&#39;d&#39;),</span>
<span class="go">                kwonlyargs=[</span>
<span class="go">                    arg(arg=&#39;e&#39;),</span>
<span class="go">                    arg(arg=&#39;f&#39;)],</span>
<span class="go">                kw_defaults=[</span>
<span class="go">                    None,</span>
<span class="go">                    Constant(value=3)],</span>
<span class="go">                kwarg=arg(arg=&#39;g&#39;),</span>
<span class="go">                defaults=[</span>
<span class="go">                    Constant(value=1),</span>
<span class="go">                    Constant(value=2)]),</span>
<span class="go">            body=[</span>
<span class="go">                Pass()],</span>
<span class="go">            decorator_list=[</span>
<span class="go">                Name(id=&#39;decorator1&#39;, ctx=Load()),</span>
<span class="go">                Name(id=&#39;decorator2&#39;, ctx=Load())],</span>
<span class="go">            returns=Constant(value=&#39;return annotation&#39;))],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.Return">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Return</code><span class="sig-paren">(</span><em class="sig-param">value</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.Return" title="Permalink to this definition">¶</a></dt>
<dd><p>A <code class="docutils literal notranslate"><span class="pre">return</span></code> statement.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;return 4&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        Return(</span>
<span class="go">            value=Constant(value=4))],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.Yield">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Yield</code><span class="sig-paren">(</span><em class="sig-param">value</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.Yield" title="Permalink to this definition">¶</a></dt>
<dt id="ast.YieldFrom">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">YieldFrom</code><span class="sig-paren">(</span><em class="sig-param">value</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.YieldFrom" title="Permalink to this definition">¶</a></dt>
<dd><p>A <code class="docutils literal notranslate"><span class="pre">yield</span></code> or <code class="docutils literal notranslate"><span class="pre">yield</span> <span class="pre">from</span></code> expression. Because these are expressions, they
must be wrapped in a <a class="reference internal" href="#ast.Expr" title="ast.Expr"><code class="xref py py-class docutils literal notranslate"><span class="pre">Expr</span></code></a> node if the value sent back is not used.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;yield x&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        Expr(</span>
<span class="go">            value=Yield(</span>
<span class="go">                value=Name(id=&#39;x&#39;, ctx=Load())))],</span>
<span class="go">    type_ignores=[])</span>

<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;yield from x&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        Expr(</span>
<span class="go">            value=YieldFrom(</span>
<span class="go">                value=Name(id=&#39;x&#39;, ctx=Load())))],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.Global">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Global</code><span class="sig-paren">(</span><em class="sig-param">names</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.Global" title="Permalink to this definition">¶</a></dt>
<dt id="ast.Nonlocal">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Nonlocal</code><span class="sig-paren">(</span><em class="sig-param">names</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.Nonlocal" title="Permalink to this definition">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">global</span></code> and <code class="docutils literal notranslate"><span class="pre">nonlocal</span></code> statements. <code class="docutils literal notranslate"><span class="pre">names</span></code> is a list of raw strings.</p>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;global x,y,z&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        Global(</span>
<span class="go">            names=[</span>
<span class="go">                &#39;x&#39;,</span>
<span class="go">                &#39;y&#39;,</span>
<span class="go">                &#39;z&#39;])],</span>
<span class="go">    type_ignores=[])</span>

<span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;nonlocal x,y,z&#39;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        Nonlocal(</span>
<span class="go">            names=[</span>
<span class="go">                &#39;x&#39;,</span>
<span class="go">                &#39;y&#39;,</span>
<span class="go">                &#39;z&#39;])],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.ClassDef">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">ClassDef</code><span class="sig-paren">(</span><em class="sig-param">name</em>, <em class="sig-param">bases</em>, <em class="sig-param">keywords</em>, <em class="sig-param">starargs</em>, <em class="sig-param">kwargs</em>, <em class="sig-param">body</em>, <em class="sig-param">decorator_list</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.ClassDef" title="Permalink to this definition">¶</a></dt>
<dd><p>A class definition.</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">name</span></code> is a raw string for the class name</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">bases</span></code> is a list of nodes for explicitly specified base classes.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">keywords</span></code> is a list of <a class="reference internal" href="keyword.html#module-keyword" title="keyword: Test whether a string is a keyword in Python."><code class="xref py py-class docutils literal notranslate"><span class="pre">keyword</span></code></a> nodes, principally for ‘metaclass’.
Other keywords will be passed to the metaclass, as per <a class="reference external" href="http://www.python.org/dev/peps/pep-3115/">PEP-3115</a>.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">starargs</span></code> and <code class="docutils literal notranslate"><span class="pre">kwargs</span></code> are each a single node, as in a function call.
starargs will be expanded to join the list of base classes, and kwargs will
be passed to the metaclass.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">body</span></code> is a list of nodes representing the code within the class
definition.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">decorator_list</span></code> is a list of nodes, as in <a class="reference internal" href="#ast.FunctionDef" title="ast.FunctionDef"><code class="xref py py-class docutils literal notranslate"><span class="pre">FunctionDef</span></code></a>.</p></li>
</ul>
<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span><span class="se">\</span>
<span class="gp">... </span><span class="s2">@decorator1</span>
<span class="gp">... </span><span class="s2">@decorator2</span>
<span class="gp">... </span><span class="s2">class Foo(base1, base2, metaclass=meta):</span>
<span class="gp">... </span><span class="s2">    pass</span>
<span class="gp">... </span><span class="s2">&quot;&quot;&quot;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        ClassDef(</span>
<span class="go">            name=&#39;Foo&#39;,</span>
<span class="go">            bases=[</span>
<span class="go">                Name(id=&#39;base1&#39;, ctx=Load()),</span>
<span class="go">                Name(id=&#39;base2&#39;, ctx=Load())],</span>
<span class="go">            keywords=[</span>
<span class="go">                keyword(</span>
<span class="go">                    arg=&#39;metaclass&#39;,</span>
<span class="go">                    value=Name(id=&#39;meta&#39;, ctx=Load()))],</span>
<span class="go">            body=[</span>
<span class="go">                Pass()],</span>
<span class="go">            decorator_list=[</span>
<span class="go">                Name(id=&#39;decorator1&#39;, ctx=Load()),</span>
<span class="go">                Name(id=&#39;decorator2&#39;, ctx=Load())])],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
</dd></dl>

</section>
<section id="async-and-await">
<h3>Async and await<a class="headerlink" href="#async-and-await" title="Permalink to this headline">¶</a></h3>
<dl class="class">
<dt id="ast.AsyncFunctionDef">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">AsyncFunctionDef</code><span class="sig-paren">(</span><em class="sig-param">name</em>, <em class="sig-param">args</em>, <em class="sig-param">body</em>, <em class="sig-param">decorator_list</em>, <em class="sig-param">returns</em>, <em class="sig-param">type_comment</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.AsyncFunctionDef" title="Permalink to this definition">¶</a></dt>
<dd><p>An <code class="docutils literal notranslate"><span class="pre">async</span> <span class="pre">def</span></code> function definition. Has the same fields as
<a class="reference internal" href="#ast.FunctionDef" title="ast.FunctionDef"><code class="xref py py-class docutils literal notranslate"><span class="pre">FunctionDef</span></code></a>.</p>
</dd></dl>

<dl class="class">
<dt id="ast.Await">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">Await</code><span class="sig-paren">(</span><em class="sig-param">value</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.Await" title="Permalink to this definition">¶</a></dt>
<dd><p>An <code class="docutils literal notranslate"><span class="pre">await</span></code> expression. <code class="docutils literal notranslate"><span class="pre">value</span></code> is what it waits for.
Only valid in the body of an <a class="reference internal" href="#ast.AsyncFunctionDef" title="ast.AsyncFunctionDef"><code class="xref py py-class docutils literal notranslate"><span class="pre">AsyncFunctionDef</span></code></a>.</p>
</dd></dl>

<div class="highlight-pycon3 notranslate"><div class="highlight"><pre><span></span><span class="gp">&gt;&gt;&gt; </span><span class="nb">print</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">dump</span><span class="p">(</span><span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s2">&quot;&quot;&quot;</span><span class="se">\</span>
<span class="gp">... </span><span class="s2">async def f():</span>
<span class="gp">... </span><span class="s2">    await other_func()</span>
<span class="gp">... </span><span class="s2">&quot;&quot;&quot;</span><span class="p">),</span> <span class="n">indent</span><span class="o">=</span><span class="mi">4</span><span class="p">))</span>
<span class="go">Module(</span>
<span class="go">    body=[</span>
<span class="go">        AsyncFunctionDef(</span>
<span class="go">            name=&#39;f&#39;,</span>
<span class="go">            args=arguments(</span>
<span class="go">                posonlyargs=[],</span>
<span class="go">                args=[],</span>
<span class="go">                kwonlyargs=[],</span>
<span class="go">                kw_defaults=[],</span>
<span class="go">                defaults=[]),</span>
<span class="go">            body=[</span>
<span class="go">                Expr(</span>
<span class="go">                    value=Await(</span>
<span class="go">                        value=Call(</span>
<span class="go">                            func=Name(id=&#39;other_func&#39;, ctx=Load()),</span>
<span class="go">                            args=[],</span>
<span class="go">                            keywords=[])))],</span>
<span class="go">            decorator_list=[])],</span>
<span class="go">    type_ignores=[])</span>
</pre></div>
</div>
<dl class="class">
<dt id="ast.AsyncFor">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">AsyncFor</code><span class="sig-paren">(</span><em class="sig-param">target</em>, <em class="sig-param">iter</em>, <em class="sig-param">body</em>, <em class="sig-param">orelse</em>, <em class="sig-param">type_comment</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.AsyncFor" title="Permalink to this definition">¶</a></dt>
<dt id="ast.AsyncWith">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">AsyncWith</code><span class="sig-paren">(</span><em class="sig-param">items</em>, <em class="sig-param">body</em>, <em class="sig-param">type_comment</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.AsyncWith" title="Permalink to this definition">¶</a></dt>
<dd><p><code class="docutils literal notranslate"><span class="pre">async</span> <span class="pre">for</span></code> loops and <code class="docutils literal notranslate"><span class="pre">async</span> <span class="pre">with</span></code> context managers. They have the same
fields as <a class="reference internal" href="#ast.For" title="ast.For"><code class="xref py py-class docutils literal notranslate"><span class="pre">For</span></code></a> and <a class="reference internal" href="#ast.With" title="ast.With"><code class="xref py py-class docutils literal notranslate"><span class="pre">With</span></code></a>, respectively. Only valid in the
body of an <a class="reference internal" href="#ast.AsyncFunctionDef" title="ast.AsyncFunctionDef"><code class="xref py py-class docutils literal notranslate"><span class="pre">AsyncFunctionDef</span></code></a>.</p>
</dd></dl>

<div class="admonition note">
<p class="admonition-title">Note</p>
<p>When a string is parsed by <a class="reference internal" href="#ast.parse" title="ast.parse"><code class="xref py py-func docutils literal notranslate"><span class="pre">ast.parse()</span></code></a>, operator nodes (subclasses
of <code class="xref py py-class docutils literal notranslate"><span class="pre">ast.operator</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">ast.unaryop</span></code>, <code class="xref py py-class docutils literal notranslate"><span class="pre">ast.cmpop</span></code>,
<code class="xref py py-class docutils literal notranslate"><span class="pre">ast.boolop</span></code> and <code class="xref py py-class docutils literal notranslate"><span class="pre">ast.expr_context</span></code>) on the returned tree
will be singletons. Changes to one will be reflected in all other
occurrences of the same value (e.g. <a class="reference internal" href="#ast.Add" title="ast.Add"><code class="xref py py-class docutils literal notranslate"><span class="pre">ast.Add</span></code></a>).</p>
</div>
</section>
</section>
<section id="ast-helpers">
<h2><a class="reference internal" href="#module-ast" title="ast: Abstract Syntax Tree classes and manipulation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ast</span></code></a> Helpers<a class="headerlink" href="#ast-helpers" title="Permalink to this headline">¶</a></h2>
<p>Apart from the node classes, the <a class="reference internal" href="#module-ast" title="ast: Abstract Syntax Tree classes and manipulation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ast</span></code></a> module defines these utility functions
and classes for traversing abstract syntax trees:</p>
<dl class="function">
<dt id="ast.parse">
<code class="sig-prename descclassname">ast.</code><code class="sig-name descname">parse</code><span class="sig-paren">(</span><em class="sig-param">source</em>, <em class="sig-param">filename='&lt;unknown&gt;'</em>, <em class="sig-param">mode='exec'</em>, <em class="sig-param">*</em>, <em class="sig-param">type_comments=False</em>, <em class="sig-param">feature_version=None</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.parse" title="Permalink to this definition">¶</a></dt>
<dd><p>Parse the source into an AST node.  Equivalent to <code class="docutils literal notranslate"><span class="pre">compile(source,</span>
<span class="pre">filename,</span> <span class="pre">mode,</span> <span class="pre">ast.PyCF_ONLY_AST)</span></code>.</p>
<p>If <code class="docutils literal notranslate"><span class="pre">type_comments=True</span></code> is given, the parser is modified to check
and return type comments as specified by <span class="target" id="index-2"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0484"><strong>PEP 484</strong></a> and <span class="target" id="index-3"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0526"><strong>PEP 526</strong></a>.
This is equivalent to adding <a class="reference internal" href="#ast.PyCF_TYPE_COMMENTS" title="ast.PyCF_TYPE_COMMENTS"><code class="xref py py-data docutils literal notranslate"><span class="pre">ast.PyCF_TYPE_COMMENTS</span></code></a> to the
flags passed to <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a>.  This will report syntax errors
for misplaced type comments.  Without this flag, type comments will
be ignored, and the <code class="docutils literal notranslate"><span class="pre">type_comment</span></code> field on selected AST nodes
will always be <code class="docutils literal notranslate"><span class="pre">None</span></code>.  In addition, the locations of <code class="docutils literal notranslate"><span class="pre">#</span> <span class="pre">type:</span>
<span class="pre">ignore</span></code> comments will be returned as the <code class="docutils literal notranslate"><span class="pre">type_ignores</span></code>
attribute of <code class="xref py py-class docutils literal notranslate"><span class="pre">Module</span></code> (otherwise it is always an empty list).</p>
<p>In addition, if <code class="docutils literal notranslate"><span class="pre">mode</span></code> is <code class="docutils literal notranslate"><span class="pre">'func_type'</span></code>, the input syntax is
modified to correspond to <span class="target" id="index-4"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0484"><strong>PEP 484</strong></a> “signature type comments”,
e.g. <code class="docutils literal notranslate"><span class="pre">(str,</span> <span class="pre">int)</span> <span class="pre">-&gt;</span> <span class="pre">List[str]</span></code>.</p>
<p>Also, setting <code class="docutils literal notranslate"><span class="pre">feature_version</span></code> to a tuple <code class="docutils literal notranslate"><span class="pre">(major,</span> <span class="pre">minor)</span></code>
will attempt to parse using that Python version’s grammar.
Currently <code class="docutils literal notranslate"><span class="pre">major</span></code> must equal to <code class="docutils literal notranslate"><span class="pre">3</span></code>.  For example, setting
<code class="docutils literal notranslate"><span class="pre">feature_version=(3,</span> <span class="pre">4)</span></code> will allow the use of <code class="docutils literal notranslate"><span class="pre">async</span></code> and
<code class="docutils literal notranslate"><span class="pre">await</span></code> as variable names.  The lowest supported version is
<code class="docutils literal notranslate"><span class="pre">(3,</span> <span class="pre">4)</span></code>; the highest is <code class="docutils literal notranslate"><span class="pre">sys.version_info[0:2]</span></code>.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>It is possible to crash the Python interpreter with a
sufficiently large/complex string due to stack depth limitations
in Python’s AST compiler.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.8: </span>Added <code class="docutils literal notranslate"><span class="pre">type_comments</span></code>, <code class="docutils literal notranslate"><span class="pre">mode='func_type'</span></code> and <code class="docutils literal notranslate"><span class="pre">feature_version</span></code>.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="ast.unparse">
<code class="sig-prename descclassname">ast.</code><code class="sig-name descname">unparse</code><span class="sig-paren">(</span><em class="sig-param">ast_obj</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.unparse" title="Permalink to this definition">¶</a></dt>
<dd><p>Unparse an <a class="reference internal" href="#ast.AST" title="ast.AST"><code class="xref py py-class docutils literal notranslate"><span class="pre">ast.AST</span></code></a> object and generate a string with code
that would produce an equivalent <a class="reference internal" href="#ast.AST" title="ast.AST"><code class="xref py py-class docutils literal notranslate"><span class="pre">ast.AST</span></code></a> object if parsed
back with <a class="reference internal" href="#ast.parse" title="ast.parse"><code class="xref py py-func docutils literal notranslate"><span class="pre">ast.parse()</span></code></a>.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>The produced code string will not necessarily be equal to the original
code that generated the <a class="reference internal" href="#ast.AST" title="ast.AST"><code class="xref py py-class docutils literal notranslate"><span class="pre">ast.AST</span></code></a> object (without any compiler
optimizations, such as constant tuples/frozensets).</p>
</div>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>Trying to unparse a highly complex expression would result with
<a class="reference internal" href="exceptions.html#RecursionError" title="RecursionError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RecursionError</span></code></a>.</p>
</div>
<div class="versionadded">
<p><span class="versionmodified added">New in version 3.9.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="ast.literal_eval">
<code class="sig-prename descclassname">ast.</code><code class="sig-name descname">literal_eval</code><span class="sig-paren">(</span><em class="sig-param">node_or_string</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.literal_eval" title="Permalink to this definition">¶</a></dt>
<dd><p>Safely evaluate an expression node or a string containing a Python literal or
container display.  The string or node provided may only consist of the
following Python literal structures: strings, bytes, numbers, tuples, lists,
dicts, sets, booleans, and <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
<p>This can be used for safely evaluating strings containing Python values from
untrusted sources without the need to parse the values oneself.  It is not
capable of evaluating arbitrarily complex expressions, for example involving
operators or indexing.</p>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p>It is possible to crash the Python interpreter with a
sufficiently large/complex string due to stack depth limitations
in Python’s AST compiler.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.2: </span>Now allows bytes and set literals.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.9: </span>Now supports creating empty sets with <code class="docutils literal notranslate"><span class="pre">'set()'</span></code>.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="ast.get_docstring">
<code class="sig-prename descclassname">ast.</code><code class="sig-name descname">get_docstring</code><span class="sig-paren">(</span><em class="sig-param">node</em>, <em class="sig-param">clean=True</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.get_docstring" title="Permalink to this definition">¶</a></dt>
<dd><p>Return the docstring of the given <em>node</em> (which must be a
<a class="reference internal" href="#ast.FunctionDef" title="ast.FunctionDef"><code class="xref py py-class docutils literal notranslate"><span class="pre">FunctionDef</span></code></a>, <a class="reference internal" href="#ast.AsyncFunctionDef" title="ast.AsyncFunctionDef"><code class="xref py py-class docutils literal notranslate"><span class="pre">AsyncFunctionDef</span></code></a>, <a class="reference internal" href="#ast.ClassDef" title="ast.ClassDef"><code class="xref py py-class docutils literal notranslate"><span class="pre">ClassDef</span></code></a>,
or <code class="xref py py-class docutils literal notranslate"><span class="pre">Module</span></code> node), or <code class="docutils literal notranslate"><span class="pre">None</span></code> if it has no docstring.
If <em>clean</em> is true, clean up the docstring’s indentation with
<a class="reference internal" href="inspect.html#inspect.cleandoc" title="inspect.cleandoc"><code class="xref py py-func docutils literal notranslate"><span class="pre">inspect.cleandoc()</span></code></a>.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.5: </span><a class="reference internal" href="#ast.AsyncFunctionDef" title="ast.AsyncFunctionDef"><code class="xref py py-class docutils literal notranslate"><span class="pre">AsyncFunctionDef</span></code></a> is now supported.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="ast.get_source_segment">
<code class="sig-prename descclassname">ast.</code><code class="sig-name descname">get_source_segment</code><span class="sig-paren">(</span><em class="sig-param">source</em>, <em class="sig-param">node</em>, <em class="sig-param">*</em>, <em class="sig-param">padded=False</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.get_source_segment" title="Permalink to this definition">¶</a></dt>
<dd><p>Get source code segment of the <em>source</em> that generated <em>node</em>.
If some location information (<code class="xref py py-attr docutils literal notranslate"><span class="pre">lineno</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">end_lineno</span></code>,
<code class="xref py py-attr docutils literal notranslate"><span class="pre">col_offset</span></code>, or <code class="xref py py-attr docutils literal notranslate"><span class="pre">end_col_offset</span></code>) is missing, return <code class="docutils literal notranslate"><span class="pre">None</span></code>.</p>
<p>If <em>padded</em> is <code class="docutils literal notranslate"><span class="pre">True</span></code>, the first line of a multi-line statement will
be padded with spaces to match its original position.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 3.8.</span></p>
</div>
</dd></dl>

<dl class="function">
<dt id="ast.fix_missing_locations">
<code class="sig-prename descclassname">ast.</code><code class="sig-name descname">fix_missing_locations</code><span class="sig-paren">(</span><em class="sig-param">node</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.fix_missing_locations" title="Permalink to this definition">¶</a></dt>
<dd><p>When you compile a node tree with <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a>, the compiler expects
<code class="xref py py-attr docutils literal notranslate"><span class="pre">lineno</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">col_offset</span></code> attributes for every node that supports
them.  This is rather tedious to fill in for generated nodes, so this helper
adds these attributes recursively where not already set, by setting them to
the values of the parent node.  It works recursively starting at <em>node</em>.</p>
</dd></dl>

<dl class="function">
<dt id="ast.increment_lineno">
<code class="sig-prename descclassname">ast.</code><code class="sig-name descname">increment_lineno</code><span class="sig-paren">(</span><em class="sig-param">node</em>, <em class="sig-param">n=1</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.increment_lineno" title="Permalink to this definition">¶</a></dt>
<dd><p>Increment the line number and end line number of each node in the tree
starting at <em>node</em> by <em>n</em>. This is useful to “move code” to a different
location in a file.</p>
</dd></dl>

<dl class="function">
<dt id="ast.copy_location">
<code class="sig-prename descclassname">ast.</code><code class="sig-name descname">copy_location</code><span class="sig-paren">(</span><em class="sig-param">new_node</em>, <em class="sig-param">old_node</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.copy_location" title="Permalink to this definition">¶</a></dt>
<dd><p>Copy source location (<code class="xref py py-attr docutils literal notranslate"><span class="pre">lineno</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">col_offset</span></code>, <code class="xref py py-attr docutils literal notranslate"><span class="pre">end_lineno</span></code>,
and <code class="xref py py-attr docutils literal notranslate"><span class="pre">end_col_offset</span></code>) from <em>old_node</em> to <em>new_node</em> if possible,
and return <em>new_node</em>.</p>
</dd></dl>

<dl class="function">
<dt id="ast.iter_fields">
<code class="sig-prename descclassname">ast.</code><code class="sig-name descname">iter_fields</code><span class="sig-paren">(</span><em class="sig-param">node</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.iter_fields" title="Permalink to this definition">¶</a></dt>
<dd><p>Yield a tuple of <code class="docutils literal notranslate"><span class="pre">(fieldname,</span> <span class="pre">value)</span></code> for each field in <code class="docutils literal notranslate"><span class="pre">node._fields</span></code>
that is present on <em>node</em>.</p>
</dd></dl>

<dl class="function">
<dt id="ast.iter_child_nodes">
<code class="sig-prename descclassname">ast.</code><code class="sig-name descname">iter_child_nodes</code><span class="sig-paren">(</span><em class="sig-param">node</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.iter_child_nodes" title="Permalink to this definition">¶</a></dt>
<dd><p>Yield all direct child nodes of <em>node</em>, that is, all fields that are nodes
and all items of fields that are lists of nodes.</p>
</dd></dl>

<dl class="function">
<dt id="ast.walk">
<code class="sig-prename descclassname">ast.</code><code class="sig-name descname">walk</code><span class="sig-paren">(</span><em class="sig-param">node</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.walk" title="Permalink to this definition">¶</a></dt>
<dd><p>Recursively yield all descendant nodes in the tree starting at <em>node</em>
(including <em>node</em> itself), in no specified order.  This is useful if you only
want to modify nodes in place and don’t care about the context.</p>
</dd></dl>

<dl class="class">
<dt id="ast.NodeVisitor">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">NodeVisitor</code><a class="headerlink" href="#ast.NodeVisitor" title="Permalink to this definition">¶</a></dt>
<dd><p>A node visitor base class that walks the abstract syntax tree and calls a
visitor function for every node found.  This function may return a value
which is forwarded by the <a class="reference internal" href="#ast.NodeVisitor.visit" title="ast.NodeVisitor.visit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">visit()</span></code></a> method.</p>
<p>This class is meant to be subclassed, with the subclass adding visitor
methods.</p>
<dl class="method">
<dt id="ast.NodeVisitor.visit">
<code class="sig-name descname">visit</code><span class="sig-paren">(</span><em class="sig-param">node</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.NodeVisitor.visit" title="Permalink to this definition">¶</a></dt>
<dd><p>Visit a node.  The default implementation calls the method called
<code class="samp docutils literal notranslate"><span class="pre">self.visit_</span><em><span class="pre">classname</span></em></code> where <em>classname</em> is the name of the node
class, or <a class="reference internal" href="#ast.NodeVisitor.generic_visit" title="ast.NodeVisitor.generic_visit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">generic_visit()</span></code></a> if that method doesn’t exist.</p>
</dd></dl>

<dl class="method">
<dt id="ast.NodeVisitor.generic_visit">
<code class="sig-name descname">generic_visit</code><span class="sig-paren">(</span><em class="sig-param">node</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.NodeVisitor.generic_visit" title="Permalink to this definition">¶</a></dt>
<dd><p>This visitor calls <a class="reference internal" href="#ast.NodeVisitor.visit" title="ast.NodeVisitor.visit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">visit()</span></code></a> on all children of the node.</p>
<p>Note that child nodes of nodes that have a custom visitor method won’t be
visited unless the visitor calls <a class="reference internal" href="#ast.NodeVisitor.generic_visit" title="ast.NodeVisitor.generic_visit"><code class="xref py py-meth docutils literal notranslate"><span class="pre">generic_visit()</span></code></a> or visits them
itself.</p>
</dd></dl>

<p>Don’t use the <a class="reference internal" href="#ast.NodeVisitor" title="ast.NodeVisitor"><code class="xref py py-class docutils literal notranslate"><span class="pre">NodeVisitor</span></code></a> if you want to apply changes to nodes
during traversal.  For this a special visitor exists
(<a class="reference internal" href="#ast.NodeTransformer" title="ast.NodeTransformer"><code class="xref py py-class docutils literal notranslate"><span class="pre">NodeTransformer</span></code></a>) that allows modifications.</p>
<div class="deprecated">
<p><span class="versionmodified deprecated">Deprecated since version 3.8: </span>Methods <code class="xref py py-meth docutils literal notranslate"><span class="pre">visit_Num()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">visit_Str()</span></code>, <code class="xref py py-meth docutils literal notranslate"><span class="pre">visit_Bytes()</span></code>,
<code class="xref py py-meth docutils literal notranslate"><span class="pre">visit_NameConstant()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">visit_Ellipsis()</span></code> are deprecated
now and will not be called in future Python versions.  Add the
<code class="xref py py-meth docutils literal notranslate"><span class="pre">visit_Constant()</span></code> method to handle all constant nodes.</p>
</div>
</dd></dl>

<dl class="class">
<dt id="ast.NodeTransformer">
<em class="property">class </em><code class="sig-prename descclassname">ast.</code><code class="sig-name descname">NodeTransformer</code><a class="headerlink" href="#ast.NodeTransformer" title="Permalink to this definition">¶</a></dt>
<dd><p>A <a class="reference internal" href="#ast.NodeVisitor" title="ast.NodeVisitor"><code class="xref py py-class docutils literal notranslate"><span class="pre">NodeVisitor</span></code></a> subclass that walks the abstract syntax tree and
allows modification of nodes.</p>
<p>The <a class="reference internal" href="#ast.NodeTransformer" title="ast.NodeTransformer"><code class="xref py py-class docutils literal notranslate"><span class="pre">NodeTransformer</span></code></a> will walk the AST and use the return value of
the visitor methods to replace or remove the old node.  If the return value
of the visitor method is <code class="docutils literal notranslate"><span class="pre">None</span></code>, the node will be removed from its
location, otherwise it is replaced with the return value.  The return value
may be the original node in which case no replacement takes place.</p>
<p>Here is an example transformer that rewrites all occurrences of name lookups
(<code class="docutils literal notranslate"><span class="pre">foo</span></code>) to <code class="docutils literal notranslate"><span class="pre">data['foo']</span></code>:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">RewriteName</span><span class="p">(</span><span class="n">NodeTransformer</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">visit_Name</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">node</span><span class="p">):</span>
        <span class="k">return</span> <span class="n">Subscript</span><span class="p">(</span>
            <span class="n">value</span><span class="o">=</span><span class="n">Name</span><span class="p">(</span><span class="nb">id</span><span class="o">=</span><span class="s1">&#39;data&#39;</span><span class="p">,</span> <span class="n">ctx</span><span class="o">=</span><span class="n">Load</span><span class="p">()),</span>
            <span class="nb">slice</span><span class="o">=</span><span class="n">Constant</span><span class="p">(</span><span class="n">value</span><span class="o">=</span><span class="n">node</span><span class="o">.</span><span class="n">id</span><span class="p">),</span>
            <span class="n">ctx</span><span class="o">=</span><span class="n">node</span><span class="o">.</span><span class="n">ctx</span>
        <span class="p">)</span>
</pre></div>
</div>
<p>Keep in mind that if the node you’re operating on has child nodes you must
either transform the child nodes yourself or call the <code class="xref py py-meth docutils literal notranslate"><span class="pre">generic_visit()</span></code>
method for the node first.</p>
<p>For nodes that were part of a collection of statements (that applies to all
statement nodes), the visitor may also return a list of nodes rather than
just a single node.</p>
<p>If <a class="reference internal" href="#ast.NodeTransformer" title="ast.NodeTransformer"><code class="xref py py-class docutils literal notranslate"><span class="pre">NodeTransformer</span></code></a> introduces new nodes (that weren’t part of
original tree) without giving them location information (such as
<code class="xref py py-attr docutils literal notranslate"><span class="pre">lineno</span></code>), <a class="reference internal" href="#ast.fix_missing_locations" title="ast.fix_missing_locations"><code class="xref py py-func docutils literal notranslate"><span class="pre">fix_missing_locations()</span></code></a> should be called with
the new sub-tree to recalculate the location information:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">tree</span> <span class="o">=</span> <span class="n">ast</span><span class="o">.</span><span class="n">parse</span><span class="p">(</span><span class="s1">&#39;foo&#39;</span><span class="p">,</span> <span class="n">mode</span><span class="o">=</span><span class="s1">&#39;eval&#39;</span><span class="p">)</span>
<span class="n">new_tree</span> <span class="o">=</span> <span class="n">fix_missing_locations</span><span class="p">(</span><span class="n">RewriteName</span><span class="p">()</span><span class="o">.</span><span class="n">visit</span><span class="p">(</span><span class="n">tree</span><span class="p">))</span>
</pre></div>
</div>
<p>Usually you use the transformer like this:</p>
<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">node</span> <span class="o">=</span> <span class="n">YourTransformer</span><span class="p">()</span><span class="o">.</span><span class="n">visit</span><span class="p">(</span><span class="n">node</span><span class="p">)</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="ast.dump">
<code class="sig-prename descclassname">ast.</code><code class="sig-name descname">dump</code><span class="sig-paren">(</span><em class="sig-param">node</em>, <em class="sig-param">annotate_fields=True</em>, <em class="sig-param">include_attributes=False</em>, <em class="sig-param">*</em>, <em class="sig-param">indent=None</em><span class="sig-paren">)</span><a class="headerlink" href="#ast.dump" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a formatted dump of the tree in <em>node</em>.  This is mainly useful for
debugging purposes.  If <em>annotate_fields</em> is true (by default),
the returned string will show the names and the values for fields.
If <em>annotate_fields</em> is false, the result string will be more compact by
omitting unambiguous field names.  Attributes such as line
numbers and column offsets are not dumped by default.  If this is wanted,
<em>include_attributes</em> can be set to true.</p>
<p>If <em>indent</em> is a non-negative integer or string, then the tree will be
pretty-printed with that indent level.  An indent level
of 0, negative, or <code class="docutils literal notranslate"><span class="pre">&quot;&quot;</span></code> will only insert newlines.  <code class="docutils literal notranslate"><span class="pre">None</span></code> (the default)
selects the single line representation. Using a positive integer indent
indents that many spaces per level.  If <em>indent</em> is a string (such as <code class="docutils literal notranslate"><span class="pre">&quot;\t&quot;</span></code>),
that string is used to indent each level.</p>
<div class="versionchanged">
<p><span class="versionmodified changed">Changed in version 3.9: </span>Added the <em>indent</em> option.</p>
</div>
</dd></dl>

</section>
<section id="compiler-flags">
<span id="ast-compiler-flags"></span><h2>Compiler Flags<a class="headerlink" href="#compiler-flags" title="Permalink to this headline">¶</a></h2>
<p>The following flags may be passed to <a class="reference internal" href="functions.html#compile" title="compile"><code class="xref py py-func docutils literal notranslate"><span class="pre">compile()</span></code></a> in order to change
effects on the compilation of a program:</p>
<dl class="data">
<dt id="ast.PyCF_ALLOW_TOP_LEVEL_AWAIT">
<code class="sig-prename descclassname">ast.</code><code class="sig-name descname">PyCF_ALLOW_TOP_LEVEL_AWAIT</code><a class="headerlink" href="#ast.PyCF_ALLOW_TOP_LEVEL_AWAIT" title="Permalink to this definition">¶</a></dt>
<dd><p>Enables support for top-level <code class="docutils literal notranslate"><span class="pre">await</span></code>, <code class="docutils literal notranslate"><span class="pre">async</span> <span class="pre">for</span></code>, <code class="docutils literal notranslate"><span class="pre">async</span> <span class="pre">with</span></code>
and async comprehensions.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 3.8.</span></p>
</div>
</dd></dl>

<dl class="data">
<dt id="ast.PyCF_ONLY_AST">
<code class="sig-prename descclassname">ast.</code><code class="sig-name descname">PyCF_ONLY_AST</code><a class="headerlink" href="#ast.PyCF_ONLY_AST" title="Permalink to this definition">¶</a></dt>
<dd><p>Generates and returns an abstract syntax tree instead of returning a
compiled code object.</p>
</dd></dl>

<dl class="data">
<dt id="ast.PyCF_TYPE_COMMENTS">
<code class="sig-prename descclassname">ast.</code><code class="sig-name descname">PyCF_TYPE_COMMENTS</code><a class="headerlink" href="#ast.PyCF_TYPE_COMMENTS" title="Permalink to this definition">¶</a></dt>
<dd><p>Enables support for <span class="target" id="index-5"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0484"><strong>PEP 484</strong></a> and <span class="target" id="index-6"></span><a class="pep reference external" href="https://www.python.org/dev/peps/pep-0526"><strong>PEP 526</strong></a> style type comments
(<code class="docutils literal notranslate"><span class="pre">#</span> <span class="pre">type:</span> <span class="pre">&lt;type&gt;</span></code>, <code class="docutils literal notranslate"><span class="pre">#</span> <span class="pre">type:</span> <span class="pre">ignore</span> <span class="pre">&lt;stuff&gt;</span></code>).</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 3.8.</span></p>
</div>
</dd></dl>

</section>
<section id="command-line-usage">
<span id="ast-cli"></span><h2>Command-Line Usage<a class="headerlink" href="#command-line-usage" title="Permalink to this headline">¶</a></h2>
<div class="versionadded">
<p><span class="versionmodified added">New in version 3.9.</span></p>
</div>
<p>The <a class="reference internal" href="#module-ast" title="ast: Abstract Syntax Tree classes and manipulation."><code class="xref py py-mod docutils literal notranslate"><span class="pre">ast</span></code></a> module can be executed as a script from the command line.
It is as simple as:</p>
<div class="highlight-sh notranslate"><div class="highlight"><pre><span></span>python -m ast <span class="o">[</span>-m &lt;mode&gt;<span class="o">]</span> <span class="o">[</span>-a<span class="o">]</span> <span class="o">[</span>infile<span class="o">]</span>
</pre></div>
</div>
<p>The following options are accepted:</p>
<dl class="cmdoption">
<dt id="cmdoption-ast-h">
<code class="sig-name descname">-h</code><code class="sig-prename descclassname"></code><code class="sig-prename descclassname">, </code><code class="sig-name descname">--help</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-ast-h" title="Permalink to this definition">¶</a></dt>
<dd><p>Show the help message and exit.</p>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-ast-m">
<code class="sig-name descname">-m</code><code class="sig-prename descclassname"> &lt;mode&gt;</code><a class="headerlink" href="#cmdoption-ast-m" title="Permalink to this definition">¶</a></dt>
<dt id="cmdoption-ast-mode">
<code class="sig-name descname">--mode</code><code class="sig-prename descclassname"> &lt;mode&gt;</code><a class="headerlink" href="#cmdoption-ast-mode" title="Permalink to this definition">¶</a></dt>
<dd><p>Specify what kind of code must be compiled, like the <em>mode</em> argument
in <a class="reference internal" href="#ast.parse" title="ast.parse"><code class="xref py py-func docutils literal notranslate"><span class="pre">parse()</span></code></a>.</p>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-ast-no-type-comments">
<code class="sig-name descname">--no-type-comments</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-ast-no-type-comments" title="Permalink to this definition">¶</a></dt>
<dd><p>Don’t parse type comments.</p>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-ast-a">
<code class="sig-name descname">-a</code><code class="sig-prename descclassname"></code><code class="sig-prename descclassname">, </code><code class="sig-name descname">--include-attributes</code><code class="sig-prename descclassname"></code><a class="headerlink" href="#cmdoption-ast-a" title="Permalink to this definition">¶</a></dt>
<dd><p>Include attributes such as line numbers and column offsets.</p>
</dd></dl>

<dl class="cmdoption">
<dt id="cmdoption-ast-i">
<code class="sig-name descname">-i</code><code class="sig-prename descclassname"> &lt;indent&gt;</code><a class="headerlink" href="#cmdoption-ast-i" title="Permalink to this definition">¶</a></dt>
<dt id="cmdoption-ast-indent">
<code class="sig-name descname">--indent</code><code class="sig-prename descclassname"> &lt;indent&gt;</code><a class="headerlink" href="#cmdoption-ast-indent" title="Permalink to this definition">¶</a></dt>
<dd><p>Indentation of nodes in AST (number of spaces).</p>
</dd></dl>

<p>If <code class="file docutils literal notranslate"><span class="pre">infile</span></code> is specified its contents are parsed to AST and dumped
to stdout.  Otherwise, the content is read from stdin.</p>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<p><a class="reference external" href="https://greentreesnakes.readthedocs.io/">Green Tree Snakes</a>, an external
documentation resource, has good details on working with Python ASTs.</p>
<p><a class="reference external" href="https://asttokens.readthedocs.io/en/latest/user-guide.html">ASTTokens</a>
annotates Python ASTs with the positions of tokens and text in the source
code that generated them. This is helpful for tools that make source code
transformations.</p>
<p><a class="reference external" href="http://leoeditor.com/appendices.html#leoast-py">leoAst.py</a> unifies the
token-based and parse-tree-based views of python programs by inserting
two-way links between tokens and ast nodes.</p>
<p><a class="reference external" href="https://libcst.readthedocs.io/">LibCST</a> parses code as a Concrete Syntax
Tree that looks like an ast tree and keeps all formatting details. It’s
useful for building automated refactoring (codemod) applications and
linters.</p>
<p><a class="reference external" href="https://parso.readthedocs.io">Parso</a> is a Python parser that supports
error recovery and round-trip parsing for different Python versions (in
multiple Python versions). Parso is also able to list multiple syntax errors
in your python file.</p>
</div>
</section>
</section>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ast</span></code> — Abstract Syntax Trees</a><ul>
<li><a class="reference internal" href="#abstract-grammar">Abstract Grammar</a></li>
<li><a class="reference internal" href="#node-classes">Node classes</a><ul>
<li><a class="reference internal" href="#literals">Literals</a></li>
<li><a class="reference internal" href="#variables">Variables</a></li>
<li><a class="reference internal" href="#expressions">Expressions</a><ul>
<li><a class="reference internal" href="#subscripting">Subscripting</a></li>
<li><a class="reference internal" href="#comprehensions">Comprehensions</a></li>
</ul>
</li>
<li><a class="reference internal" href="#statements">Statements</a><ul>
<li><a class="reference internal" href="#imports">Imports</a></li>
</ul>
</li>
<li><a class="reference internal" href="#control-flow">Control flow</a></li>
<li><a class="reference internal" href="#function-and-class-definitions">Function and class definitions</a></li>
<li><a class="reference internal" href="#async-and-await">Async and await</a></li>
</ul>
</li>
<li><a class="reference internal" href="#ast-helpers"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ast</span></code> Helpers</a></li>
<li><a class="reference internal" href="#compiler-flags">Compiler Flags</a></li>
<li><a class="reference internal" href="#command-line-usage">Command-Line Usage</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="parser.html"
                        title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">parser</span></code> — Access Python parse trees</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="symtable.html"
                        title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">symtable</span></code> — Access to the compiler’s symbol tables</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../bugs.html">Report a Bug</a></li>
      <li>
        <a href="https://github.com/python/cpython/blob/3.9/Doc/library/ast.rst"
            rel="nofollow">Show Source
        </a>
      </li>
    </ul>
  </div>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="symtable.html" title="symtable — Access to the compiler’s symbol tables"
             >next</a> |</li>
        <li class="right" >
          <a href="parser.html" title="parser — Access Python parse trees"
             >previous</a> |</li>

    <li><img src="../_static/py.png" alt=""
             style="vertical-align: middle; margin-top: -1px"/></li>
    <li><a href="https://www.python.org/">Python</a> &#187;</li>
    

    <li>
      <a href="../index.html">3.9.6 Documentation</a> &#187;
    </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="language.html" >Python Language Services</a> &#187;</li>
    <li class="right">
        

    <div class="inline-search" style="display: none" role="search">
        <form class="inline-search" action="../search.html" method="get">
          <input placeholder="Quick search" type="text" name="q" />
          <input type="submit" value="Go" />
          <input type="hidden" name="check_keywords" value="yes" />
          <input type="hidden" name="area" value="default" />
        </form>
    </div>
    <script type="text/javascript">$('.inline-search').show(0);</script>
         |
    </li>

      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 2001-2021, Python Software Foundation.
    <br />

    The Python Software Foundation is a non-profit corporation.
<a href="https://www.python.org/psf/donations/">Please donate.</a>
<br />
    <br />

    Last updated on Jun 28, 2021.
    <a href="https://docs.python.org/3/bugs.html">Found a bug</a>?
    <br />

    Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 2.3.1.
    </div>

  </body>
</html>